C++ 错误:无法调用构造函数
我在ns2中加入了新模块,用于评估视频传输。我对一些文件进行了必要的更改,如agent.h、agent.cc、makefile等。 在这个过程中,你会被错误缠住。 错误是:C++ 错误:无法调用构造函数,c++,linux,ns2,C++,Linux,Ns2,我在ns2中加入了新模块,用于评估视频传输。我对一些文件进行了必要的更改,如agent.h、agent.cc、makefile等。 在这个过程中,你会被错误缠住。 错误是: myevalvid/myudp.cc: In member function ‘virtual void myUdpAgent::sendmsg(int, AppData*, const char*)’: myevalvid/myudp.cc:56:123: warning: format ‘%d’ expects arg
myevalvid/myudp.cc: In member function ‘virtual void myUdpAgent::sendmsg(int, AppData*, const char*)’:
myevalvid/myudp.cc:56:123: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat]
myevalvid/myudp.cc:78:123: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat]
make: *** No rule to make target `myevalvid/myevalvid_sink.o ', needed by `ns'. Stop.
代码是
#include "myudp.h"
#include "rtp.h"
#include "random.h"
#include "address.h"
#include "ip.h"
static class myUdpAgentClass : public TclClass {
public:
myUdpAgentClass() : TclClass("Agent/myUDP") {}
TclObject* create(int, const char*const*) {
return (new myUdpAgent());
}
} class_myudp_agent;
myUdpAgent::myUdpAgent() : id_(0), openfile(0)
{
bind("packetSize_", &size_);
}
void myUdpAgent::sendmsg(int nbytes, AppData* data, const char* flags)
{
Packet *p;
int n;
char buf[100]; //added by smallko
if (size_)
n = nbytes / size_;
else
printf("Error: myUDP size = 0\n");
if (nbytes == -1) {
printf("Error: sendmsg() for UDP should not be -1\n");
return;
}
// If they are sending data, then it must fit within a single packet.
if (data && nbytes > size_) {
printf("Error: data greater than maximum myUDP packet size\n");
return;
}
double local_time = Scheduler::instance().clock();
while (n-- > 0) {
p = allocpkt();
hdr_cmn::access(p)->size() = size_;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
hdr_cmn::access(p)->sendtime_ = local_time; // (smallko)
if(openfile!=0){
hdr_cmn::access(p)->frame_pkt_id_ = id_++;
sprintf(buf, "%-16f id %-16d udp %-16d\n", local_time, hdr_cmn::access(p)->frame_pkt_id_, hdr_cmn::access(p)->size()-28);
fwrite(buf, strlen(buf), 1, BWFile);
//printf("%-16f id %-16d udp %-16d\n", local_time, hdr_cmn::access(p)->frame_pkt_id_, hdr_cmn::access(p)->size()-28);
}
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 ==strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
n = nbytes % size_;
if (n > 0) {
p = allocpkt();
hdr_cmn::access(p)->size() = n;
hdr_rtp* rh = hdr_rtp::access(p);
rh->flags() = 0;
rh->seqno() = ++seqno_;
hdr_cmn::access(p)->timestamp() =
(u_int32_t)(SAMPLERATE*local_time);
hdr_cmn::access(p)->sendtime_ = local_time; // (smallko)
if(openfile!=0){
hdr_cmn::access(p)->frame_pkt_id_ = id_++;
sprintf(buf, "%-16f id %-16d udp %-16d\n", local_time, hdr_cmn::access(p)->frame_pkt_id_, hdr_cmn::access(p)->size()-28);
fwrite(buf, strlen(buf), 1, BWFile);
//printf("%-16f id %-16d udp %-16d\n", local_time, hdr_cmn::access(p)->frame_pkt_id_, hdr_cmn::access(p)->size()-28);
}
// add "beginning of talkspurt" labels (tcl/ex/test-rcvr.tcl)
if (flags && (0 == strcmp(flags, "NEW_BURST")))
rh->flags() |= RTP_M;
p->setdata(data);
target_->recv(p);
}
idle();
}
int myUdpAgent::command(int argc, const char*const* argv)
{
if(argc ==2) { //added by smallko
if (strcmp(argv[1], "closefile") == 0) {
if(openfile==1)
fclose(BWFile);
return (TCL_OK);
}
}
if (argc ==3) { //added by smallko
if (strcmp(argv[1], "set_filename") == 0) {
strcpy(BWfile, argv[2]);
BWFile = fopen(BWfile, "w");
openfile=1;
return (TCL_OK);
}
}
return (UdpAgent::command(argc, argv));
}
请帮助我整理错误。正如错误所说,您不能直接调用构造函数,因为这一行似乎在尝试:
UdpAgent::UdpAgent();
您可能只是想删除该行。该构造函数已在构造函数的开头被(隐式)调用。如果您想明确说明,可以将
UdpAgent()
放在初始化器列表的开头。编辑,因为这与“make”完全无关。@billz:声明声明了一个静态变量class\u myudp\u agent
,并定义了类。@MikeSeymour现在我看到了。很好学。感谢您不知道为什么会有UdpAgent::UdpAgent()代码>那里。您知道吗?对于第56行和第78行的错误,如果您使用%d
,您实际上必须传递一个int
,或一个无符号int
,其值范围为int
,或更小的整数类型。该行为未定义为通过任何其他操作。如果int
和long
具有相同的大小,它通常会起作用,但您仍然应该修复它,因为您不知道何时将在其他编译器上重新编译代码或更改编译器设置,最后得到大小不同的int
和long
。我猜我们还假设UdpAgent
是TclClass
的基类,否则这会使sense@MattMcNabb:否,我假设UdpAgent
是myUdpAgent
(发布代码中缺少其定义)。错误消息似乎表明情况就是这样。@Mike Seymour:按照您的建议进行了更正并解决了第一个错误。对于第二个错误,请建议进行精确修改。@user257489:使用%-16ld
而不是%-16d
来格式化长的值。或者使用std::stringstream
或std::to_string
,而不是乱搞C库。@Mike Seymour:现在我在编辑代码时发现了错误。请建议一些关于无规则的东西来创建目标。