无法从字符串流接收信息

问题描述 投票:1回答:1

所以我试图使用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

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

c++ c++17
1个回答
1
投票

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

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

所以我要做的就是

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

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

© www.soinside.com 2019 - 2024. All rights reserved.