I am a newbie trying to code a Menu class and I want it to be able to call functions that are part of other classes. I have sought thoroughly on the internet, but the answers that I've found doesn't fit my needs.
Here is a straightforward piece of code that defines my problem:
#include <iostream>
class Class1;//forward declaration
typedef void (Class1::*FunctionPtr_t)();//type: Pointer to a function in Class1 scope
class Class1
{
public:
void function1()
{std::cout << "function1 executed!";}//Test function to execute from Class2
};
class Class2
{
public:
FunctionPtr_t myfcnptr = NULL;//Pointer to a function in Class1
};
int main()
{
Class2 myclass2;
myclass2.myfcnptr = &Class1::function1;//Assign function to pointer (Everything OK here)
(myclass2.*myfcnptr)();//Compilation Error: 'myfcnptr' was not declared in this scope
}
I have messed around, made both classes friend and used scope operators almost everywhere. I know the solution must be quite easy and that I'm making a ridiculous mistake, but I don't catch it! Thank you for your patience ;)
Class1to call a member function of that class. Do you really need to mess around with function pointers?