【问题标题】:Add a vector<string> field to a cMessage将 vector<string> 字段添加到 cMessage
【发布时间】:2021-06-06 16:13:26
【问题描述】:

我正在构建一个包含向量作为一些字段的自定义 cMessage。我对 intdouble 向量没有任何问题,但是对于 string 向量,我得到一个错误。下面是重现问题的示例消息定义。

cplusplus {{
#include <vector>

typedef std::vector<int> IntVector;
typedef std::vector<string> StrVector;
}};

class IntVector { @existingClass; };
class StrVector { @existingClass; };

message sampleMessage extends cMessage
{
    IntVector SampleIntVector;
    StrVector SampleStrVector;
}

在我的代码中,我有以下代码块

sampleMessage *msg = new sampleMessage();
vector<int> intVect = {1,2};
vector<string> stringVect;
string inputString = "dummy string";
stringVect.push_back(inputString);
msg->setSampleIntVector(intVect);
msg->setSampleStrVector(stringVect);

使用 OMNeT++ 版本 6.0 pre10,在第 7 行,我收到以下错误,提示 cMessage 期待 vector&lt;char *&gt;

error: no viable conversion from 'vector<std::string>' to 'const vector<char *>'

我还尝试了 OMNeT++ 5.6.2 版并收到了不同的错误消息。为清楚起见,文件 testModel_m.cc 由 OMNeT++ 生成。

testModel_m.cc:170:13: error: use of overloaded operator '<<' is ambiguous (with operand types 'std::ostream' (aka 'basic_ostream<char>') and 'const std::__cxx11::basic_string<char>')
        out << *it;
        ~~~ ^  ~~~
testModel_m.cc:2040:45: note: in instantiation of function template specialization 'operator<<<std::__cxx11::basic_string<char>, std::allocator<std::__cxx11::basic_string<char> > >' requested here
        case 1: {std::stringstream out; out << pp->getSampleStrVector(); return out.str();}
                                            ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/basic_string.h:6416:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
    operator<<(basic_ostream<_CharT, _Traits>& __os,
    ^
testModel_m.cc:158:22: note: candidate function [with T = std::__cxx11::basic_string<char>]
inline std::ostream& operator<<(std::ostream& out,const T&) {return out;}

如果我将向量更改为char *,它可以工作,但对于我的用例,我需要string 的向量,因为我在向量中搜索值并使用char *,这不起作用很好。

有没有办法让vector&lt;string&gt; 字段作为自定义 cMessage 的一部分?

【问题讨论】:

  • 你使用什么版本的 OMNeT++?
  • 我使用的是 6.0 预览版 10。
  • 我安装了 5.6.2 版并得到了不同的错误。用与每个测试的 OMNeT++ 版本相关的错误更新了问题。

标签: omnet++


【解决方案1】:

错误use of overloaded operator '&lt;&lt;' is ambiguous 的解决方法是在cplusplus {{ }} 块内添加自己的operator&lt;&lt; 定义,例如:

cplusplus {{
#include <vector>

typedef std::vector<int> IntVector;
typedef std::vector<std::string> StrVector;

std::ostream& operator<<(std::ostream &os, const StrVector& vec) {
    std::stringstream out; 
    for (auto i : vec) {
        out << i << ", ";
    }
    return os << out.str();
};
}};
    
class IntVector { @existingClass; };
class StrVector { @existingClass; };

message sampleMessage {
    IntVector SampleIntVector;
    StrVector SampleStrVector;
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多