Problem calling pointer to member function (Solved)

Started by
4 comments, last by Windhawk 17 years, 4 months ago
I have a menu class, which contains a vector of buttons (another class). Each button also has assigned to it a pointer to a member function of the menu class (the effect of the button). These are then called according to which button is clicked / pressed. But I can't seem to manage to call the member functions from the pointers correctly (code won't compile). Here's the menu class showing relevant bits:

class cMenu {
    private:
        void display();
        void addButton(string text, mePointer mePtr);
        bool doButton(vector<button>::const_iterator bi);
        
        void meNewGame();
        void meLoadGame();
        void meSettings();
        void meReturn();
        void meQuit();
        
        vector<button> mButtons;

};



and here's relevant parts of the button class with the typedef, to make valid pointers to the cMenu::meFoos():

typedef void (cMenu::*mePointer)();
class button {
    public:
        friend class cMenu;
    private:
        string text;
        mePointer mePtr;
};



Buttons are added to the array like so:

        addButton("Return", &cMenu::meReturn);
void cMenu::addButton(string text, mePointer mePtr) {
    button temp;
    SDL_Color c;
    
    temp.text = text;
    temp.mePtr = mePtr;
    
    mButtons.push_back(temp);
}

//and for ref
void cMenu::meReturn() {
    menu.finish();
}



I try to call the pointed to functions like so:

void cMenu::display() {
    for (vector<button>::const_iterator i=mButtons.begin(); i!=mButtons.end(); ++i) {
        if (doButton(i))
            (i->*mePtr)();
    }
}



This gives me the compiler error that "`mePtr' undeclared (first use this function) ". I've tried messing around with the call, but I can't seem to get it right. Am I doing something fundamentally wrong here? [Edited by - Windhawk on December 14, 2006 9:21:51 PM]
Advertisement
Try this

i->mePtr();

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Quote:Original post by LessBread
Try this

i->mePtr();


"must use .* or ->* to call pointer-to-member function..."
Quote:(i->*mePtr)();

operator ->* will only work if i is a button* but it is instead a iterator of a vector of buttons and so you have to use ((*i).*mePtr)()

Quote:Try this

i->mePtr();


To my knowledge that wont work as mePtr still needs to be dereferensed the problem is that SC++L classes dont overload the ->* operator
Quote:Original post by Windhawk
Quote:Original post by LessBread
Try this

i->mePtr();


"must use .* or ->* to call pointer-to-member function..."


You want:

(this->*(i->mePtr))();


Hint: It will bitch about mePtr being private in this context.
Edit: As Julian90 points out, ->* is not implemented by iterators.
Edit: As uavfun in IRC points out, mePointer is a cMenu::*, not cButton::*
Thanks to MaulingMonkey and uavfun on IRC, this is the correct version:

(this->*i->mePtr)();

This topic is closed to new replies.

Advertisement