我认为这主要是一个语法错误(虽然在编译期间没有捕获,但只有在运行时才会发生错误)
我正在尝试开发一个事件驱动的架构,在其中我保留一个 eventReceiver 的向量并尝试从该向量中调用它们。
通用IO.h
class CommonIO {
public:
typedef void (*incomingMsgReceiver)(char*);
void registerForIncomingMsg(incomingMsgReceiver);
void incomingMsgReceived(char*);
}
通用IO.cpp
std::vector<void (*)(char*)> listeners;
void CommonIO::registerForIncomingMsg(incomingMsgReceiver receiverFunction) {
listeners.push_back(receiverFunction);
}
void CommonIO::incomingMsgReceived(char* buf) {
for (unsigned int j = 0; j < listeners.size(); j++) {
listeners[i](buf); //error, segmentation fault..how do i call it?
}
}
主文件
class Main {
public:
static void msgReceived(char*);
void mainLoop();
}
主文件
void Main::msgReceived(char* msg)
{
printf("\n\n --------->>>>> %s \n\n" , msg);
}
void Main::mainLoop()
{
CommonIO::incomingMsgReceiver receiver = &Main::msgReceived;
CommonIO::getInstance()->registerForIncomingMsg( receiver );
}
异步进程调用incomingMsgReceived 函数
该程序编译得很好..但是在:listenersi; 线。什么是正确的语法?