• Advertisement
Sign in to follow this  

Compile Errors W/ Function Pointer Array & Class Member Function

This topic is 4249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok Im using Visual Studio EE Edition with C++. An example of my code and logic is below. MyClass.h FILE
class MyClass
{
public:
   MyClass();//Constructor
   void function1();
   void function2();
private:
   void (*fPtr[2])();// declare function pointer array

};


MyClass.cpp FILE
MyClass::MyClass()
  {
    fPtr[0] = &MyClass::function1;
    fPtr[1] = &MyClass::function2;
  }

void MyClass::function1()
  {
  }
void MyClass::function2()
   {
   }



I get a compile error of //---------------------------------------------------------------------------error C2440: '=' : cannot convert from 'void (__thiscall GameMap::* )(void)' to 'void (__cdecl *)(void)' There is no context in which this conversion is possible \\---------------------------------------------------------------------------// Now I never dealt with function pointers forClass member functions so thats probally where most of my mistakes are. For all I know I might not even be able to make a function pointer for a class. But I cant understand what I am doing wrong. So any help I would REALLY appreciate. :)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by PB Baller27
Now I never dealt with function pointers forClass member functions so thats probally where most of my mistakes are. For all I know I might not even be able to make a function pointer for a class.


You can, but they work differently and are not compatible with pointers to non-members.

void (MyClass::*fPtr[2])();

The call syntax is also different, since you need to provide an object at that point: (object.*member_pointer)() or (object_pointer->*member_pointer)() rather than just non_member_pointer().


Check the C++ FAQ Lite.

Share this post


Link to post
Share on other sites
Thanks Fruny! :) However yet another bug has come out of its hole.
When dereferencing the function ptr to execute a function,I get a compile error.

CODE:
*fPtr[0];




//----------------------------------------------------------------------------\\
error C2171: '*' : illegal on operands of type 'void (__thiscall GameMap::* )(void)'
\\---------------------------------------------------------------------------//





Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
The call syntax is also different, since you need to provide an object at that point: (object.*member_pointer)() or (object_pointer->*member_pointer)() rather than just non_member_pointer().


Share this post


Link to post
Share on other sites
um... yea.... Sorry about that. My brain wasn't exaclty "on" last night. However thank you very much for the help and the program is now working. Have a great 4th of JULY!!! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement