首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向协议缓冲区添加重复字段

向协议缓冲区添加重复字段
EN

Stack Overflow用户
提问于 2015-01-02 13:34:30
回答 1查看 21.8K关注 0票数 3

我在C++中使用了一个协议缓冲区模板,包括以下消息:

代码语言:javascript
复制
message StringTable {
   repeated bytes s = 1;
}

我试图为现有数据添加一个新值,如下所示:

代码语言:javascript
复制
pb.stringtable().s().Add(replace_key);

但是,这会在编译时产生错误(在OS X上使用clang ):

代码语言:javascript
复制
test.cpp:51:4: error: member function 'Add' not viable: 'this' argument 
  has type 'const ::google::protobuf::RepeatedPtrField< ::std::string>', 
  but function is not marked const
                    pb.stringtable().s().Add(replace_key);
                    ^~~~~~~~~~~~~~~~~~~~

有什么线索吗?我是C++的新手,所以可能会犯一个愚蠢的错误。

编辑:

使用访问器会产生类似的错误:

代码语言:javascript
复制
pb.stringtable().add_s(replace_key);

在以下方面的成果:

代码语言:javascript
复制
test.cpp:51:21: error: no matching member function for call to 'add_s'
                        pb.stringtable().add_s(replace_key);
                        ~~~~~~~~~~~~~~~~~^~~~~
./osmformat.pb.h:3046:26: note: candidate function not viable: 'this' argument has type 'const ::StringTable', but method is not marked const
inline void StringTable::add_s(const ::std::string& value) {
                     ^
./osmformat.pb.h:3050:26: note: candidate function not viable: 'this' argument has type 'const ::StringTable', but method is not marked const
inline void StringTable::add_s(const char* value) {
                     ^
./osmformat.pb.h:3043:36: note: candidate function not viable: requires 0 arguments, but 1 was provided
inline ::std::string* StringTable::add_s() {
                               ^
./osmformat.pb.h:3054:26: note: candidate function not viable: requires 2 arguments, but 1 was provided
inline void StringTable::add_s(const void* value, size_t size) {
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-02 14:28:59

问题解决了。

默认情况下,现有的StringTable不是可变的。但是,使用mutable_访问器可以这样做:

代码语言:javascript
复制
pb.mutable_stringtable().add_s(replace_key);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27743063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档