Click to See Complete Forum and Search --> : Why is my member function static?
Gorgor
October 2nd, 2002, 09:19 AM
In a member function, I get the (in VC) error:
error C2352: 'CSomeOtherClass::SomeMemberFunction' : illegal call of non-static member function
Upon investigation, it appears the member function of my class is static for some reason, and that 'CSomeOtherClass::SomeMemberFunction' (member function of the other class) is not, thus the error, which is fine.
However, I don't even have the "static" keyword in my own class definition anywhere, and checking all the ancestors (about 8), none of them have my function defined, so I know it isn't an override inheriting some static somewhere. (I even added an x to the name to make sure.)
What possibilities am I looking for that would make my member function static? It isn't some compiler option since a dummy class I created had no problem calling that other function, meaning the dummy class was created non-static, as expected.
To sum up (pseudocode):
class MyClass : InheritingClass {
MyMemberFunction();
}
MyClass::MyMemberFunction()
{
CSomeOtherClass::SomeMemberFunction(); // Generates error
}
Note that I am calling CSomeOtherClass::SomeMemberFunction() and not someVariable->SomeMemberFunction().
That function is declared virtual (not pure) and may or may not be overridden, but it definitely exists in CSomeOtherClass.
So, is it generating the error because I am trying to directly call a virtual function (even if defined in that base class?) or is it something else still?
Gorgor
October 2nd, 2002, 09:28 AM
I think I understand what's going on now.
Since I'm calling a function directly and not as part of an object, i.e.:
CSomeOtherClass::AFunction()
instead of
instanceVariable->AFunction())
AFunction() will have no object to reference, thus it can't access any of its own member variables other than static ones.
Thus my class is actually non static, in spite of the error message, and the error message applies to the CSomeOtherClass because I'm calling a function that isn't declared static itself.
I.e. the example the help files gives for the error doesn't apply, that the caller is static and trying to call a non-static. Rather, the caller is non-static, and is calling a non-static function in what would be an unsafe manner.
Ironically, the function I'm calling only uses local auto variables and one static member variable, so it could be declared static.
PaulWendt
October 2nd, 2002, 09:48 AM
There was probably a double-post so I'm duplicating my response
from the other thread here:
error C2352: 'CSomeOtherClass::SomeMemberFunction' : illegal
call of non-static member function
This message doesn't mean that your function is static; it says
that you're CALLING it as if it were static. You illegally called a
non-static member function. Look at how you're calling the
function:
CSomeOtherClass::SomeMemberFunction'
The only way you can use this syntax is if SomeMemberFunction
is a statis member function of CSomeOtherClass or if
CSomeOtherClass is a parent class if the member function you're
defining.
If SomeMemberFunction() will have an object associated with it,
then pass in the object to your function or create one locally.
Otherwise, it SHOULD be a static member function....
--Paul
Andreas Masur
October 2nd, 2002, 10:15 AM
To add some more information to Paul's correct answer:
In C++ every member function has a hidden parameter - the so-called 'this' pointer which will be automatically passed to the function. C++ is able to associate a function with a particular instance of an object by means of the 'this' pointer. Member functions access member variables through the 'this' pointer...
class CFoo
{
public:
void Function()
{
m_iInteger = 0;
}
private:
int m_iInteger;
};
If you compile this code it will be compiled as
class CFoo
{
public:
void Function(CFoo *this)
{
this->m_iInteger = 0;
}
private:
int m_iInteger;
};
You are trying to call the member function of any object. The compiler does not know which function of which object you want to call. Therefore it complains about it. Static member functions on the contrary do not receive a 'this' pointer and the standard guarantees that there will be only one function for all the objects of the class...
In this case the compiler knows exactly which function to call since there is only one and therefore does not complain...
Gorgor
October 2nd, 2002, 11:17 AM
Thanks for the responses. Although I figured out the problem, the code (very large project, > 80 subprojects) is liberal with such calls of static member functions beyond typical usage like CSomeClass::getInstance() and I got a bit confused as to what would work and what wouldn't.
Sadly, the code also has that function as a protected one, so I can't get at it anyway as it turns out. As it is a core piece of code used in dozens of projects and maintained by a group in another country, changing the one function to public (which would be reasonable) might become a problem...
Gorgor
October 2nd, 2002, 11:25 AM
In this case, the error means I, a non-static member function, am calling one that should be, but isn't, static.
See the confusing VC++ help on the error:
Compiler Error C2352
'class::function' : illegal call of non-static member function
The specified nonstatic member function was called in a static member function.
The following is an example of this error:
class X
{
public:
static void func1();
void func2();
static void func3()
{
func1(); // OK, calls static func1
func2(); // error, calls nonstatic func2
}
};
The description and example are one of two ways to generate this error evidently. The other is what I stumbled into. The help message could be appended, ", or you are attempting to call a non-static member function as a (whatever technobabble describes a member function called by the ClassName:: method instead of instanceVariable-> method.)"
Andreas Masur
October 2nd, 2002, 11:42 AM
Originally posted by Gorgor
Compiler Error C2352
'class::function' : illegal call of non-static member function
The specified nonstatic member function was called in a static member function.
The following is an example of this error:
class X
{
public:
static void func1();
void func2();
static void func3()
{
func1(); // OK, calls static func1
func2(); // error, calls nonstatic func2
}
};
Well...basically the above showed sample is more of the other limitations static functions have. Static functions can only call static member variables or other static functions. That is why the above call of 'func2()' fails. Internally it is the same reason. Since a static function does not have a 'this' pointer it cannot call a standard member function because of the reasons I described in my earlier post...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.