Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting filed of a Message inside Message Protobuf

I have the following protobuf definition:

message CBMessage {

    required int32 type = 1; //defines the kind of message that we send
    optional RepoMessage repomessage = 2;

    message RepoMessage { 
        optional int32 nodeid = 1;
        optional int32 timestampsec = 2;
        optional int32 timestampmicrosec = 3;
    }
}

As you can note, the repomessage filed, is a filed of the "outer" message CBMessage.

I would like to access to nodeid filed (of the "inner" message RepoMessage) in order to modify this parameter. So I create a CBMessage object:

CBTxMessages::CBMessage* cbmsg;
this->cbmsg = new CBTxMessages::CBMessage;

And I have tried to modify the nodeid filed in this way:

this->cbmsg->repomessage().set_nodeid(message[0]);

Yet, I get the following error when I compile:

error: pasar ‘const CBTxMessages::CBMessage_RepoMessage’ como el argumento ‘this’ de ‘void CBTxMessages::CBMessage_RepoMessage::set_nodeid(google::protobuf::int32)’ descarta a los calificadores [-fpermissive]

(The error is in Spanish but I think that you can understand).A possible translation will be:

"the 'this' argument of 'void CBTxMessages :: CBMessage_RepoMessage :: set_nodeid (google :: protobuf :: int32)' discards the qualifiers")

I have investigated it and found that the problem is related to the fact that the "basic getter" of the repomessage filed returns (repomessage()) is returned as const reference (thus it can not be modified directly), but I do wish to modify it.

How can I solve this ?

Thanks.

like image 458
user1799803 Avatar asked Nov 05 '12 10:11

user1799803


1 Answers

Instead of

this->cbmsg->repomessage().set_nodeid(message[0]);

you'll need

this->cbmsg->mutable_repomessage()->set_nodeid(message[0]);

to set the repomessage field directly. repomessage() will return a const CBMessage_RepoMessage& that cannot be modified.

like image 58
πάντα ῥεῖ Avatar answered Nov 14 '22 13:11

πάντα ῥεῖ