0

So I have a vector of a struct object containing a function pointer to an external class.

//Menu.h

#include "Byte.h"

struct menuItem
{
    Byte (Byte::*funcPoint)(Byte&);
    string descript;
};

class Menu
{
private:
    vector<menuItem> menuVect;
    void runSelection(Byte&, Byte&);
public:
    Menu();
    void addMenu(string Description, Byte (Byte::*f)(Byte&));
    void runMenu(Byte&, Byte&);
    void waitKey();
};

I can assign new instances of function pointers as seen here:

void Menu::addMenu(string Description, Byte(Byte::*f)(Byte&))
{
    menuVect.push_back({f, Description});
}

But when it comes time execute any given function within the vector, I get an error

void Menu::runSelection(Byte& bX, Byte& bY)
{
int select;

    cin >> select;
    if (select > 0 && select < menuVect.size() + 1) {
        cout << bX.toInt();
        menuVect.at(select - 1).funcPoint();
    }
    else
        exit(0);
    waitKey();

}

Not sure if its necessary, but I'll include main as well, as one of the function pointers being assigned to the vector

Main

#include "Menu.h"

int main()
{
Menu m;
Byte b1(7);
Byte b2(3);

    m.addMenu("1. Func 1", Byte::add);
    m.addMenu("2. Func 2", Byte::sub);
    m.addMenu("3. Func 3", Byte::mul);
    m.addMenu("4. Func 4", Byte::div);
    
    m.runMenu(b1, b2);
    
    return 0;

}
Byte Byte::add(int val)
{
    Byte b(this->toInt() + val);
    return b;
}

I have already found an obvious work around, by simply not having the functions within an object

The particular errors I'm getting are:

Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type

(field)std::vector Menu::menuVect

Error C3867 'Byte::add': non-standard syntax; use '&' to create a pointer to member

But nothing I find when I look this up helps.

Arudama
  • 1
  • 2
  • `Byte::\*funcPoint` and there are other typos – 463035818_is_not_an_ai Feb 03 '23 at 08:18
  • There's no need to escape characters in code blocks. Please read [the formatting help](https://stackoverflow.com/editing-help). – Some programmer dude Feb 03 '23 at 08:19
  • seems like some copy paste process introduced \ in the code – 463035818_is_not_an_ai Feb 03 '23 at 08:19
  • As for *one* of your problems, to get a pointer to a member function you *must* use the pointer-to operator `&`. Member functions doesn't decay to pointers like non-member functions. Also, you need an actual object to call the functions on, you can't just call member function pointers because they don't have any object information built-in. – Some programmer dude Feb 03 '23 at 08:21
  • you posted too much code. You should reduce it to one [mcve] and include the complete error message. – 463035818_is_not_an_ai Feb 03 '23 at 08:21
  • And I really recommend you learn about [`std::function`](https://en.cppreference.com/w/cpp/utility/functional/function), and [lambdas](https://en.cppreference.com/w/cpp/language/lambda). Those two will help you create better generic function handling code. – Some programmer dude Feb 03 '23 at 08:22
  • it looks odd that the error you posted would be the first. The first error to be expected is about missing `&`. But, code is not fixed by looking at code and having heureka moments and fixing the bugs, code is fixed by reading the complete error message and understand what it says. If you dont understand it no problem, include it in the question then we can explain – 463035818_is_not_an_ai Feb 03 '23 at 08:23
  • `menuVect.at(select - 1).funcPoint();` seems to be attempting to call the member function without the required object of the type that the function is a member of. – Avi Berger Feb 03 '23 at 08:23
  • When you call a pointer to a member function, you must provide a `this` pointer as the first parameter. – Ivan Venkov Feb 03 '23 at 08:34
  • @463035818_is_not_a_number I had already included the error message at the bottom of the page. I have included the other detail of the error in the edit. Also, where should I post the missing ampersand (&) sign? – Arudama Feb 03 '23 at 16:47
  • @AviBerger could you possibly clarify what I should edit in the code to achieve this? – Arudama Feb 03 '23 at 16:57
  • Its more a matter of design. non-static member functions are different than free functions. Likewise regarding their pointers. For a plain function pointer, provide the parameters & you can call it. Not so for non-static member function pointers. That requires an object of the appropriate class to call. It makes no sense to call one outside of that context. Maybe this isn't what you need. If it is, you know what object you need & you have to make it available so you can use it for the call - either by bundling ( a reference to) it in a function object or otherwise. – Avi Berger Feb 04 '23 at 00:02
  • @AviBerger ah, so I was mistaken thinking that I can implement function pointers in this particular manner to begin with? good to know. thanks for the help! – Arudama Feb 04 '23 at 21:47

0 Answers0