1

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

1 Answer 1

1
menu.addOption("Connect",connect(screen,&menu));

You're passing the result of connect, which is void, as the second parameter to addOption. Try:

menu.addOption("Connect",connect);

--

I think you're looking to send both a function and some parameters for that function, so consider adding two extra parameters to addOption such that it's signature is like:

addOption(string optionName,void (*f)(SDL_Surface*, MenuBar*), SDL_Surface*, MenuBar*)

And call f with the parameters given inside addOption. Then you call addOption like so:

menu.addOption("Connect",connect, screen, &menu);
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Now it makes a lot of sens, thanks a lot. It is working perfectly. I got confused because I didn't remember I had that run function, and this was the function supposed to get the parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.