1

我正在尝试为每条消息发送一个“结构”向量,但是在定义消息字段时会生成以下错误:

进入目录 '/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;

我的应用程序.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;

4

1 回答 1

2

您遇到类型不匹配:在MyApp.h您定义 typeUpdateTable时,您在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 标头,或者UpdateTable从 App 中删除定义并包含而是消息。

于 2019-11-12T14:02:01.713 回答