消息生成错误,运营商的过度重载<<

时间:2015-12-14 14:38:10

标签: omnet++

使用nedtool / omnet(4.6)生成消息.cc和头文件后出现以下错误

  

'operator<<'的歧义重载(操作数类型是'std :: ostream {aka std :: basic_ostream}'和'const std :: basic_string')CurrentNeighboursMessage_m.cc

这是消息类:CurrentNeighboursMessage.msg

cplusplus {{
 #include  <vector>
 #include <string>

 typedef std::vector<std::string> StringVector;
}}

    class noncobject StringVector;

    packet CurrentNeighboursMessage {
        StringVector currentNeighbours;
    }

生成的CurrentNeighboursMessage_m.cc中的错误的相应代码:

错误在这一行:

  

out&lt;&lt; *它;

  // operator<< for std::vector<T>
    template<typename T, typename A>
    inline std::ostream& operator<<(std::ostream& out, const std::vector<T,A>& vec)
    {
        out.put('{');
        for(typename std::vector<T,A>::const_iterator it = vec.begin(); it != vec.end(); ++it)
        {
            if (it != vec.begin()) {
                out.put(','); out.put(' ');
            }

            out << *it; 
        }
        out.put('}');

        char buf[32];
        sprintf(buf, " (size=%u)", (unsigned int)vec.size());
        out.write(buf, strlen(buf));
        return out;
    }

有人知道解决方案吗?

1 个答案:

答案 0 :(得分:0)

https://groups.google.com/forum/#!msg/omnetpp/9Cw6F6ws_pc/isW42Rh0WG4J

找到解决方案

要么不使用typedef,要么使用此解决方法:

cplusplus {{
#include  <vector>
#include <string>
//workaround
typedef std::string MyString;

inline std::ostream & operator << (std::ostream & os, const MyString & s) {
        std::operator<<(os, s);        
        return os;
}
typedef std::vector<MyString> StringVector;

}}
class noncobject StringVector;
packet CurrentNeighboursMessage {
    StringVector currentNeighbours;
}