I had this problem some time ago and I gave up but lately it returned.
#include <iostream>
class element2D;
class node2D
{
public:
void (element2D::*FunctionPtr)();
void otherMethod()
{ std::cout << "hello" << std::endl;
((this)->*(this->FunctionPtr))(); //ERROR<-------------------
}
};
class element2D
{
public:
node2D myNode;
void doSomething(){ std::cout << "do something" << std::endl; }
};
int main()
{
element2D myElement;
myElement.myNode.FunctionPtr = &element2D::doSomething; //OK
((myElement).*(myElement.myNode.FunctionPtr))(); //OK
return 0;开发者_运维知识库
}
I'm getting error at marked line:
pointer to member type 'void (element2D::)()' incompatible with object type 'node2D'
I would be really thankful for help. There was similar question today which partially helped me: link. But it doesn't seem to be the full answer to my problem.
Actually these two problems have only one difference - point where the function is called.
Thanks for your time
"this" is a pointer to node2D but FunctionPtr refers to a member of element2D -- that is the error.
#if 0 // broken version
void otherMethod()
{ std::cout << "hello" << std::endl;
((this)->*(this->FunctionPtr))(); //ERROR<-------------------
}
#else // fixed version
void otherMethod( element2D * that )
{ std::cout << "hello" << std::endl;
((that)->*(this->FunctionPtr))();
}
#endif
Then you call it with something like:
myElement.myNode.otherMethod( &myElement );
There are things you could do to improve on this, but this should get you started.
You need a pointer to element2D object if you wish to call a pointer to element2D member. Consider using Boost.Function or Boost.Signals2 for the functionality that you want.
精彩评论