【发布时间】:2015-08-17 18:58:44
【问题描述】:
我正在尝试在 C++ 中使用新的 protobuf Map 功能。
以下是在 Ubuntu 14.04 上使用 gcc 4.9.2 C++14 和 protobuf 3.0.0-alpha-4 完成的:
消息定义:
message TestMap {
map<string,uint64> map1 = 1;
}
然后,我尝试编译以下示例程序:
auto test = debug::TestMap::default_instance();
auto map = test.mutable_map1();
string key = "key";
uint64 val = 20;
map[key] = val;
使用 [] 语法访问地图对 std::unordered_map 完全正常。但是 protobuf 实现总是会产生以下编译器错误:
error: no match for ‘operator[]’ (operand types are ‘google::protobuf::Map<std::basic_string<char>, long unsigned int>*’ and ‘std::string {aka std::basic_string<char>}’)
不明白为什么不知道这个操作符,因为头文件google::protobuf::Map很清楚,这应该是一个初级操作。
你知道这里出了什么问题吗?我欢迎任何使用新 protobuf 地图的示例,因为在研究了几个小时后我还没有在网上找到任何示例。
【问题讨论】:
-
您的
map变量似乎是一个指针。你试过*map[key]吗? -
@Pixelchemist:你说得对,感谢您指出这一点。
*map[key]不起作用,但*(map)[key]起作用。很基本的一个!我一直看错了方向。 -
@Pixelchemist:你想发布答案还是我应该这样做?
标签: c++ hashmap protocol-buffers c++14