Reputation: 63
I'm currently working on some code and need to make something like an event handler that I can register explicit events and store them into a vector that I can loop through in my main listen() function. I'm missing something about pointers that I cant pinpoint with docs and a google search and need help figuring out why my compilers asking for a pointer to a member.
I've tried creating a typedef with a member function definition but I have the problem of it not compiling currently with a "non standard syntax, put an & to create a pointer to a member." error.
class Obj {
private:
typedef int (Obj::*Event) (std::vector<std::string> in);
std::vector<Event> events;
int exampleEvent(std::vector<std::string> input);
public:
Obj();
int regEvent(Event ev);
int listen();
}
example event code
int Obj::exampleEvent(std::vector<std::string> input)
{
// heres my app logic
return 0;
}
register events in constructor
Obj::Obj()
{
regEvent(exampleEvent); // exampleEvent: non-standard syntax; use
//'&' to create a pointer to member
}
listen, and add event to vector.
int Obj::regEvent(Event ev)
{
events.push_back(ev);
return 0;
}
// listen for command input
int Obj::listen()
{
// get input
string str;
getline(cin, str);
vector<string> input = split(str, " ");
// loop through events
for (auto ev : events)
{
ev(input); // <-- Term does not evaluate to function taking 1 arg.
}
return 0;
}
Upvotes: 0
Views: 37
Reputation: 32586
Obj::Obj()
{
regEvent(exampleEvent); // exampleEvent: non-standard syntax; use
//'&' to create a pointer to member
}
must be
Obj::Obj()
{
regEvent(&Obj::exampleEvent);
}
and
for (auto ev : events)
{
ev(input); // <-- Term does not evaluate to function taking 1 arg.
}
must be
for (auto ev : events)
{
(this->*ev)(input);
}
Upvotes: 1