Hi Randy,

Thanks for your help!

1. Yeah, I did mean obj.write(binaryProtocol); to be obj.write(protocolOut). I 
was trying different stuff, which resulted in a copy/paste error.

2. The protocolOut.get() did the trick. There's a thing I don't understand. I 
was looking at the source files before, particularly at TBinaryProtocol.h and 
TProtocol.h and did not see where the .get() method was defined. Where is the 
definition located?

-Jack

-----Original Message-----
From: Randy Abernethy [mailto:[email protected]] 
Sent: Tuesday, October 29, 2013 7:34 PM
To: [email protected]
Subject: Re: serializing binary data in C++

Hello Jack,

These two lines of code are the issue:

    shared_ptr<TBinaryProtocol> protocolOut(new TBinaryProtocol(transportOut));
    obj.write(binaryProtcol);


1. I take it that the obj.write(binaryProtcol); is meant to be: obj.write( 
protocolOut);

2. The problem with the write is that the Thrift generated write function wants 
a TProtocol * If you change your code to the following I think you will have 
success:

    obj.write(protocolOut.get());

Best,
Randy


On Tue, Oct 29, 2013 at 6:23 PM, Chan, Jack <[email protected]>wrote:

> Hi,
>
> I'm having a lot of trouble serializing data. What am I doing wrong?
>
> std::string serialize(ContactsList& obj, std::string filename) {
>     shared_ptr<TMemoryBuffer> transportOut(new TMemoryBuffer());
>     shared_ptr<TBinaryProtocol> protocolOut(new 
> TBinaryProtocol(transportOut));
>     obj.write(binaryProtcol);
>     std::string serialized_string = transportOut->getBufferAsString();
>     return serialized_string;
> }
>
> This is the method I call from another method. I expect to get back a 
> serialized binary string which I can write out to disk. Inside this 
> serialize method, I create a TMemory buffer, then I wrap it in a 
> TBinaryProtocol, and then the object's write method, which will write 
> itself into the memory buffer. Then, I get that buffer back as a string.
> I'd then write out the serialized string to disk.
>
> I get this error:
> error: no matching function for call to 
> 'addressbook::ContactsList::write(boost::shared_ptr<apache::thrift::pr
> otocol::TBinaryProtocolT<apache::thrift::transport::TTransport>
> >&)
>
> As well as this note:
> note:   no known conversion for argument 1 from
> 'boost::shared_ptr<apache::thrift::protocol::TBinaryProtocolT<apache::
> thrift::transport::TTransport>
> >' to 'apache::thrift::protocol::TProtocol*
>
> I'm using Apache Thrift 1.0-dev, C++ 98 if these things make a difference.
>
> Am I understanding everything correctly (probably not cuz it's not 
> working)? What's the correct way to serialize an object? Pleeeeeaase 
> edify me. Here is the stackoverflow link:
> http://stackoverflow.com/questions/19672155/apache-thrift-serializatio
> n-in-c
>
> Thanks,
> Jack
>
>

Reply via email to