我在 NS2 中实现计时器时遇到问题。我想实现一个计时器,它在到期时只打印"过期"。代码如下:
法普:
class Fap;
class HelloTimer : public TimerHandler
{
public:
HelloTimer(Fap* fapAgent) : TimerHandler(){agent = fapAgent;}
protected:
Fap* agent;
inline virtual void expire(Event* e);
};
class Fap : public Agent
{
public:
Fap(void);
int filter[ADDRESS_SPACE];
int address;
int netId;
int requestedIp;
bool configured;
nsaddr_t requestor;
nsaddr_t initiator;
HelloTimer helloTimer;
TcpAgent* tcp;
NsObject* ll;
void recv(Packet* p, Handler*);
void sendHello(void);
int command(int, const char* const*);
};
fap.cc
void HelloTimer:: expire(Event* event)
{
//agent->sendHello();
printf("expiredn");
}
int Fap::command(int argc, const char* const* argv)
{
Tcl& tcl = Tcl::instance();
if(argc == 2)
{
if(strcmp(argv[1], "hello") == 0)
{
helloTimer.resched(30); //problem occurs here
helloTimer.status(); //no problem with this
return (TCL_OK);
}
}
else if(argc == 3)
{
if(strcmp(argv[1], "set-ll") == 0)
{
NsObject* temp;
temp = (NsObject*)(TclObject::lookup(argv[2]));
ll = temp;
return (TCL_OK);
}
}
else
{
printf("invalid number of args");
}
Agent::command(argc,argv);
return(TCL_OK);
}
仅添加了代码的相关部分。每当我尝试使用 resched() 重新安排计时器时,都会发生分段错误。我错过了什么吗?
这是
类名冲突的结果。只需在计时器类名上加上前缀即可。