简体   繁体   English

OMNeT ++ TicToc扩展字符串消息

[英]OMNeT++ TicToc extension string message

I am starting with OMNeT++ and C++ by going through the TicToc tutorial. 我将通过TicToc教程来开始使用OMNeT ++和C ++。

I would now like to make a modification on the behavior of one of the submodules Tic or Toc, specifically in handleMessage(). 我现在想对Tic或Toc子模块之一的行为进行修改,特别是在handleMessage()中。

Currently, messages are handled by forwarding the received message to the other submodule without any manipulation of the message. 当前,通过将接收到的消息转发到另一个子模块来处理消息,而无需对该消息进行任何操作。 Now, I would like to change this so that Tic checks the incoming message's string and if the value is "String 1" then if will generate a new message with string value "String 2" and send it to Toc. 现在,我想更改此设置,以便Tic检查传入消息的字符串,如果值是“ String 1”,则if将生成一个新消息,其字符串值为“ String 2”,并将其发送给Toc。

However when I do this I get and error "comparison between distinct pointer types 'cMessage' and 'const char*' lacks a cast. 但是,当我这样做时,我得到了错误“不同的指针类型'cMessage'和'const char *'之间的比较缺少转换。

This is the code: 这是代码:

void Tic::handleMessage(cMessage *msg)
{
    if (msg == "String 1")
    {
       cMessage *msg2 = new cMessage ("String 2");
       send(msg2,"out");
    }
}

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

I found the solution: 我找到了解决方案:

if (strcmp("String 1", msg->getName())==0)
{}

We know very little about cMessage , but perhaps you meant 我们对cMessage知之甚少,但也许您的意思是

if (*msg == "String 1")

Because that would compare the value of the cMessage object pointer-to by msg (a pointer), to the string literal value. 因为这样会将msg指向的cMessage对象指针的值(一个指针)与字符串文字值进行比较。

As you had it, you try to compare a pointer to a sting literal (which decays to char const* in this context), which makes no sense (see also How to compare pointers? ). 有了它,您尝试比较指向字符串的指针(在这种情况下会衰减为char const* ),这没有任何意义(另请参见如何比较指针? )。


Update Upon reading more here http://www.omnetpp.org/doc/omnetpp/api/index.html it doesn't look like the above would work. 更新在此处阅读更多内容后, http: //www.omnetpp.org/doc/omnetpp/api/index.html看起来上述内容不起作用。

In fact, you might want to read some of the member properties (info, detailed info, encapsulated cPacket etc.) in order to inspect the message 实际上,您可能想阅读一些成员属性(信息,详细信息,封装的cPacket等),以便检查消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM