我正在尝试为每条消息发送一个“结构”向量,但是在定义消息字段时会产生以下错误:
进入目录'/home/veins/workspace.omnetpp/veins/src' 静脉/模块/应用程序/clustertraci/ClusterTraCI11p.cc 静脉/模块/应用程序/clustertraci/ClusterTraCI11p.cc:160:40:错误:没有从“向量”到“常量向量”的可行转换 frameOfUpdate-> setUpdateTable(updateTable);
我阅读了OMnet ++手册的第6章,但我不知道如何解决此问题。
消息代码(MyMessage.msg):
cplusplus {{
#include "veins/base/utils/Coord.h"
#include "veins/modules/messages/BaseFrame1609_4_m.h"
#include "veins/base/utils/SimpleAddress.h"
#include <iostream>
#include <vector>
struct updateTableStruct {
int car;
char update;
};
typedef std::vector<updateTableStruct> UpdateTable;
}}
namespace veins;
class BaseFrame1609_4;
class noncobject Coord;
class noncobject UpdateTable;
class LAddress::L2Type extends void;
packet ClusterMessageUpdate extends BaseFrame1609_4 {
LAddress::L2Type senderAddress = -1;
int serial = 0;
UpdateTable updateTable;
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);
}
}
MyApp.h中用于分析的部分代码:
struct updateTableStruct {
int car;
char update;
};
typedef std::vector<updateTableStruct> UpdateTable;
UpdateTable updateTable;
答案 0 :(得分:2)
您遇到类型不匹配:在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
定义并改为包含App标头,或者删除{{1 }}从应用程序中定义,并包含消息。