简体   繁体   中英

cannot call member function without object error

I have a static funcA in ClassA which calls non-static funcB in classA. Although I gave object to the funcB call I still get the error: cannot call member function without object

void* ClassA::funcA(void *arg)
{
   ClassA *pC = reinterpret_cast<ClassA *>(arg);

   funcB(pc);
}

void* ClassA::funcB(ClassA *arg)
{

}

what is the reason for that?

A static class method can be called without an object, like you're doing.
A regular class method needs to be called on an object, like this: objectInstance.classMethod( arguments go here ) or objectPointer->classMethod( arguments go here )

Try this (after changing the signature of funcB in your class declaration to match):

void* ClassA::funcA(void *arg)
{
   ClassA *pC = reinterpret_cast<ClassA *>(arg);

   pC->funcB();
}

void* ClassA::funcB()
{
    ...
}

The problem is that to call funcB it should be done via some object like:

pC->funcB(pC);

Actually this kind of code is more like C than C++ because if you are calling a method on an object you don't need to pass it as a parameter.

You're calling from a static method, so there's no receiver object in the scope.

Consequently, you cannot call a non-static method.

You need an object, which will receive the message: o.funcB(pc);

its not a good idea to call a member function from static function , The reason why it errors out here is that the functionB is invoked from static method . the static method cannot invoke non static member functions . The reason is static function operates on classes not on objects .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM