c++ - 错误 : cannot call constructor

标签 c++ linux ns2

我已将新模块包含到 ns2 中以评估视频传输。我对 agent.h、agent.cc、makefile 等文件进行了必要的更改。 在 make 遇到错误。 错误是:

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() 放在初始化程序列表的开头。

关于c++ - 错误 : cannot call constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22558637/

相关文章:

c++ - c++ shared_ptr中的相互破坏

c++ - 对角展平矩阵的邻域索引计算

java - Window/linux 路径组件分离

c++ - ns2的c++代码中的节点在哪里处理数据包?

c++ - VC++ Debug模式:批量编辑 std::vector<int> 值?

android - 我可以使用什么来分析 android 的 C++ 模块代码

linux - 使用 SED 从文本文件中的值生成特定格式(逐行)

c 命令 system() 的输出可以进入文件而不是显示在控制台上吗?

java - 计算 NS2 中 UDP 数据包的哈希值

c++ - 如何修改 ns2 模拟器中节点的行为?