I have troubles fixing a little problem I have. I have two classes. One is called MenuBar, in this class there is a vector array of the other object (MenuOption). MenuBar contains a method that create a new MenuOption and add it to the array. The constructor of MenuOption needs two arguments: a string(for the name) and a function(the action that this option performs). I want the function received to be of that form:
void method(SDL_Surface* arg1, MenuBar* arg2);
So I repaired the double inclusion problem by adding "class MenuBar;" just after my #include "MenuBar.h". But I still have an error, it says "error: invalid use of void expression" at the line I marked in the below code of main.cpp.
Now my complete code looks like that:
main.cpp
void connect(SDL_Surface* arg1, MenuBar* arg2);
void about(SDL_Surface* arg1, MenuBar* arg2);
int main()
{
SDL_Surface* screen;
MenuBar menu(/*initialization*/);
menu.addOption("Connect",connect(screen,&menu));//<---------
menu.addOption("About",about(screen,&menu)); //<---------
}
void connect(SDL_Surface* arg1, MenuBar* arg2)
{...}
void about(SDL_Surface* arg1, MenuBar* arg2)
{...}
MenuBar.h
#include "MenuOption.h"
class MenuBar
{
public:
...
void addOption(string optionName,void (*f)(SDL_Surface*, MenuBar*) );
private:
vector<MenuOption> optionList;
}
MenuBar.cpp
void MenuBar::addOption(string optionName,void (*f)(SDL_Surface*, MenuBar*) )
{
MenuOption tempOption(optionName,f);
optionList.push_back(tempOption);
}
MenuOption.h
#include "MenuBar.h"
class MenuBar;
class MenuOption
{
public:
MenuOption(string optionName,void (*f)(SDL_Surface*, MenuBar*) );
void (*run)(SDL_Surface*, MenuBar*);
private:
string name;
}
MenuOption.cpp
MenuOption::MenuOption(string optionName,void (*f)(SDL_Surface*, MenuBar*) )
{
name = optionName;
run = f;
}
Thanks for the help!
Philou231