我在C++中使用了一个协议缓冲区模板,包括以下消息:
message StringTable {
repeated bytes s = 1;
}我试图为现有数据添加一个新值,如下所示:
pb.stringtable().s().Add(replace_key);但是,这会在编译时产生错误(在OS X上使用clang ):
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++的新手,所以可能会犯一个愚蠢的错误。
编辑:
使用访问器会产生类似的错误:
pb.stringtable().add_s(replace_key);在以下方面的成果:
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) {发布于 2015-01-02 14:28:59
问题解决了。
默认情况下,现有的StringTable不是可变的。但是,使用mutable_访问器可以这样做:
pb.mutable_stringtable().add_s(replace_key);https://stackoverflow.com/questions/27743063
复制相似问题