我有一个功能
void newEvent(void (*onRun)(), std::string eventName)
我这样称呼
newEvent((*Event1)(), "Event1");
其中 Event1() 显然是一个 void 函数。这是我的构建输出:
/home/mrasicci/Programming/SSCE/main.cpp||In member function ‘void MenuState::enter()’:|
/home/mrasicci/Programming/SSCE/main.cpp|45|error: invalid use of member function (did you forget the ‘()’ ?)|
||=== Build finished: 1 errors, 0 warnings ===|
我已经尝试将其更改newEvent((*Event1)(), "Event1");
为其他东西,例如newEvent((*Event1), "Event1");
或没有newEvent(Event1(), "Event1");
任何newEvent(Event1, "Event1");
一个有效,所有这些都给出了不同的错误。这是给出错误的示例代码,预期的输出将是“Event1 触发”,但它不能编译:
#include <iostream>
#include <vector>
namespace TGE
{
class EventListener
{
public:
typedef struct
{
void (*run)();
bool triggered;
std::string name;
} Event;
void newEvent(void (*onRun)(), std::string eventName)
{
Event newEvent;
newEvent.name = eventName;
newEvent.triggered = false;
newEvent.run = onRun;
eventStack.push_back(newEvent);
}
void trigger(std::string eventName)
{
std::vector<Event>::iterator itr;
for(itr = eventStack.begin(); itr != eventStack.end(); itr++)
{
if(itr->name == eventName) itr->triggered = true;
}
}
protected:
std::vector<Event> eventStack;
};
}
class MenuState : protected TGE::EventListener
{
public:
void enter()
{
newEvent((*Event1)(), "Event1");
trigger("Event1");
std::vector<Event>::iterator itr;
for(itr = eventStack.begin(); itr != eventStack.end(); itr++)
{
if(itr->triggered)
{
itr->run();
itr->triggered = false;
}
}
}
void Event1()
{
std::cout << "Event1";
}
};
int main()
{
MenuState* menuState = 0;
menuState = new MenuState();
menuState->enter();
return 0;
}