Clearly you're doing _something_ wrong... but what? There's more
surrounding code (if nothing else, you can't have a function called
"main-1"), and I suspect that code might be the issue. For example,
one place I've gotten caught before is if you have

foo &a = ...;
foo &b = ...;
a = b;

That copies b into a, rather than assigning a to a reference to b.

What happens if you change your function to take a pointer rather than
a reference?

  -ilia


On Mon, Aug 19, 2013 at 1:25 PM, Alvaro Aguilera
<[email protected]> wrote:
> The problem is that if I use main-1 the string value is lost when leaving
> the scope of the attribute_value() function.
> Am I doing something wrong?
>
>
>
>
> On Monday, August 19, 2013 7:17:56 PM UTC+2, Ilia Mirkin wrote:
>>
>> Perhaps I'm dense, but what's the problem? That gdb doesn't print the
>> exact type info that you expected? That could well be the difference
>> between -O2 and -O0...
>>
>> On Mon, Aug 19, 2013 at 9:55 AM, Alvaro Aguilera
>> <[email protected]> wrote:
>> > Hello,
>> >
>> > I'm having a strange problem when setting an string value in a protocol
>> > buffer. Keep in mind that my experience in C++ and PB is rather limited.
>> > The .proto file looks more or less like this:
>> >
>> > ----------------------
>> > package buffers;
>> >
>> > message AttributeValue {
>> >
>> >     enum Type {
>> >         INT64   = 0;
>> >         UINT64  = 1;
>> >         INT32   = 2;
>> >         UINT32  = 3;
>> >         FLOAT   = 4;
>> >         DOUBLE  = 5;
>> >         INVALID = 6;
>> >         STRING  = 7;
>> >     }
>> >
>> >     required   Type type = 1;
>> >     optional   int32 i32 = 2;
>> >     optional   int64 i64 = 3;
>> >     optional uint32 ui32 = 4;
>> >     optional uint64 ui64 = 5;
>> >     optional     float f = 6;
>> >     optional    double d = 7;
>> >     optional  string str = 8;
>> >
>> > }
>> >
>> > ----------------------
>> >
>> > I also have a function to convert a class "VariableDatatype" into an
>> > AttributeValue buffer. It looks like this:
>> >
>> > void BufferConverter::attribute_value(const VariableDatatype &s_atv,
>> > buffers::AttributeValue &b_atv)
>> > {
>> >     switch(s_atv.type()) {
>> >     case VariableDatatype::Type::STRING:
>> >         b_atv.set_type(buffers::AttributeValue::STRING);
>> >         b_atv.set_str(s_atv.str());
>> >         break;
>> >     }
>> > }
>> >
>> >
>> > In the main function I try to create a protocol buffer containing the
>> > string
>> > stored in VariableDatatype by using the attribute_value() function in
>> > the
>> > way shown below:
>> >
>> > main-1()
>> > {
>> >     buffers::AttributeValue *bs = new buffers::AttributeValue();
>> >     c.attribute_value(ds, *bs);
>> >
>> > }
>> >
>> > This doesn't work.
>> > If I print the value of b_atv inside the attribute_value function using
>> > gdb
>> > I get something like:
>> >
>> > gdb print b_atv
>> > 26 = (buffers::AttributeValue &) @0x6496b0: {<google::protobuf::Message>
>> > =
>> > {<google::protobuf::MessageLite> = {
>> >
>> >
>> > However, when I print *bs from inside main-1, I get something different:
>> >
>> > gdb print *bs
>> > $36 = {<google::protobuf::Message> = {<google::protobuf::MessageLite> =
>> > {_vptr.MessageLite = 0x642650 <vtable for buffers::AttributeValue+16>
>> >
>> > I can work around the problem by using the following main-2:
>> >
>> > main-2()
>> > {
>> >     buffers::AttributeValue *bs = new buffers::AttributeValue();
>> >     buffers::AttributeValue &bss = *bs;
>> >
>> >     c.attribute_value(ds, bss);
>> > }
>> >
>> > but that's not very elegant. Can someone point me to the right way to
>> > handle
>> > this?
>> >
>> > Thank you
>> > A.
>> >
>> >
>> >
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Protocol Buffers" group.
>> > To unsubscribe from this group and stop receiving emails from it, send
>> > an
>> > email to [email protected].
>> > To post to this group, send email to [email protected].
>> > Visit this group at http://groups.google.com/group/protobuf.
>> > For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to