Adding to a Protocol Buffers repeated field
19,951
Problem solved.
The existing StringTable isn't mutable by default. However, using the mutable_ accessors makes it so:
pb.mutable_stringtable().add_s(replace_key);
Author by
Richard Fairhurst
Updated on June 04, 2022Comments
-
Richard Fairhurst almost 2 years
I'm working in C++ with a Protocol Buffer template including the following message:
message StringTable { repeated bytes s = 1; }
I'm attempting to add a new value to the existing data, like so:
pb.stringtable().s().Add(replace_key);
However, this generates an error on compilation (using clang on OS X):
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); ^~~~~~~~~~~~~~~~~~~~
Any clues? I'm very much a C++ newbie so may be making a dumb error.
Edit:
Using the accessors produces a similar error:
pb.stringtable().add_s(replace_key);
results in:
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) {