无法从字符串流接收信息

时间:2019-11-26 00:09:49

标签: c++ c++17

所以我试图使用std::stringstream将信息序列化为字符串,但是编译器不喜欢我。

enum PacketType : unsigned int {
        PacketType_unknown = 0,
        PacketType_ping,
        PacketType_server_welcome,
        PacketType_client_greetings,
    };
std::stringstream ss;
unsigned int v;
PacketType p;
ss << (unsigned int)somevalue;
// error here
ss >> p;

错误是:

no match for 'operator>>' (operand types are 'std::stringstream' {aka 
'std::__cxx11::basic_stringstream<char>'} and 'PacketType')GCC

编辑:忘了添加太多东西,因为我认为这并不重要

1 个答案:

答案 0 :(得分:1)

我终于弄明白了,我的代码不起作用的原因是因为...

PacketType!= unsigned int。 PacketType是它自己的类型。即使它基于unsigned int

所以我要做的就是

unsigned int s;
ss >> s;
somevalue = static_cast<PacketType>(s);

虽然还是很奇怪... 不应该PacketType继承unsigned int

相关问题