Omnet++ 如何发送一个";结构";每个消息的向量?
我试图为每条消息发送一个“struct”向量,但在定义消息字段时,生成了以下错误: 正在进入目录“/home/venles/workspace.omnetpp/venles/src” 静脉/模块/应用程序/clustertraci/ClusterTraCI11p.cc Velse/modules/application/clustertraci/ClusterTraCI11p.cc:160:40:错误:从“vector”到“const vector”没有可行的转换 frameOfUpdate->setUpdateTable(updateTable) 我阅读了OMnet++手册的第6章,但我不明白如何解决这个问题 消息代码(MyMessage.msg):Omnet++ 如何发送一个";结构";每个消息的向量?,omnet++,veins,Omnet++,Veins,我试图为每条消息发送一个“struct”向量,但在定义消息字段时,生成了以下错误: 正在进入目录“/home/venles/workspace.omnetpp/venles/src” 静脉/模块/应用程序/clustertraci/ClusterTraCI11p.cc Velse/modules/application/clustertraci/ClusterTraCI11p.cc:160:40:错误:从“vector”到“const vector”没有可行的转换 frameOfUpdate->
cplusplus{{
#包括“Velse/base/utils/Coord.h”
#包括“纹理/模块/消息/基本框架1609_4_m.h”
#包括“Velse/base/utils/SimpleAddress.h”
#包括
#包括
结构updateTableStruct{
int轿车;
字符更新;
};
typedef std::vector UpdateTable;
}}
脉;
类别BaseFrame1609_4;
类对象坐标;
类非对象可更新;
类LAddress::L2Type扩展了void;
数据包ClusterMessageUpdate扩展BaseFrame1609\u 4{
LadAddress::L2Type senderAddress=-1;
int系列=0;
可更新的可更新的;
MyApp.cc:
void ClusterTraCI11p::handleSelfMsg(cMessage* msg) {
if (ClusterMessage* frame = dynamic_cast<ClusterMessage*>(msg)) {
ClusterMessageUpdate* frameOfUpdate = new ClusterMessageUpdate;
populateWSM(frameOfUpdate, CH2);
frameOfUpdate->setSenderAddress(myId);
frameOfUpdate->setUpdateTable(updateTable);
sendDelayedDown(frameOfUpdate, uniform(0.1, 0.02));
}
else {
DemoBaseApplLayer::handleSelfMsg(msg);
}
}
void ClusterTraCI11p::handleSelfMsg(cMessage*msg){
if(ClusterMessage*frame=dynamic_cast(msg)){
ClusterMessageUpdate*frameOfUpdate=新建ClusterMessageUpdate;
populateWSM(frameOfUpdate,CH2);
frameOfUpdate->setSenderAddress(myId);
frameOfUpdate->setUpdateTable(updateTable);
sendDelayedDown(框架更新,统一(0.1,0.02));
}
否则{
DemoBaseApplayer::handleSelfMsg(msg);
}
}
MyApp.h中用于分析的部分代码:
struct updateTableStruct {
int car;
char update;
};
typedef std::vector<updateTableStruct> UpdateTable;
UpdateTable updateTable;
struct updateTableStruct{
int轿车;
字符更新;
};
typedef std::vector UpdateTable;
可更新的可更新的;
您遇到了类型不匹配:在MyApp.h
中定义了类型UpdateTable
,然后在MyMessage.h
中定义了类型。虽然这两种类型具有相同的内容,并且似乎具有相同的名称,但我假设实际情况并非如此:一种类型是UpdateTable
(在基于消息生成的文件的全局范围中定义),另一个是MyApp::UpdateTable
(在应用程序中定义,假设您在显示的代码中省略了类定义)
因此,类型是不同的,并且它们不能被隐式地相互转换。在这种情况下,这可能看起来有点违反直觉,因为它们有完全相同的定义,但它们没有相同的名称。下面的示例说明了推理:共享同一定义的两个不同类型不应该是必需的可以相互隐式转换:
struct Coordinate {
int x;
int y;
};
struct Money {
int dollars;
int cents;
};
void test() {
Coordinate c;
Money m = c;
}
给出以下错误消息:
test.cc:13:8: error: no viable conversion from 'Coordinate' to 'Money'
Money m = c;
^ ~
test.cc:6:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'Coordinate' to 'const Money &' for 1st argument
struct Money {
^
test.cc:6:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'Coordinate' to 'Money &&' for 1st argument
struct Money {
^
1 error generated.
编辑:
您的特定问题的解决方案是删除其中一个定义,并在使用它时包含其余的定义,因此您可以从消息中删除
UpdateTable
定义并包含应用程序标题,或者从应用程序中删除UpdateTable
定义并包含消息。请将代码直接嵌入到您的问题中。添加了什么类型的updateTable
?@Horstinator代码。“MyApp.h”是“ClusterTraCI11p.h”和“MyApp.cc”是ClusterTraCI11p.cc。我将文件名放在一个通用的方式中,在您的评论之后,我理解这些名称可能会在理解代码时造成混乱。请问,是否可以以任何方式转换它们?我编辑了我的答案,以详细说明具体的修复。从技术上讲,可以转换这两种类型,但这是一种本质上不安全的解决方案根据你的解释,我设法解决了这个问题。非常感谢。
test.cc:13:8: error: no viable conversion from 'Coordinate' to 'Money'
Money m = c;
^ ~
test.cc:6:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'Coordinate' to 'const Money &' for 1st argument
struct Money {
^
test.cc:6:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'Coordinate' to 'Money &&' for 1st argument
struct Money {
^
1 error generated.