3

データのシリアル化に苦労しています。私は何を間違っていますか?

std::string serialize(ContactsList& obj, std::string filename) {
    shared_ptr<TMemoryBuffer> transportOut(new TMemoryBuffer());
    shared_ptr<TBinaryProtocol> protocolOut(new TBinaryProtocol(transportOut));
    obj.write(protocolOut);
    std::string serialized_string = transportOut->getBufferAsString();
    return serialized_string;
}

これは、別のメソッドから呼び出すメソッドです。ディスクに書き出せるシリアル化されたバイナリ文字列が返されることを期待しています。この serialize メソッド内で、TMemory バッファを作成し、それを TBinaryProtocol でラップしてから、メモリ バッファに自身を書き込むオブジェクトの write メソッドをラップします。次に、そのバッファーを文字列として取得します。次に、シリアル化された文字列をディスクに書き出します。

次のエラーが表示されます。

error: no matching function for call to ‘addressbook::ContactsList::write(boost::shared_ptr<apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TTransport> >&)

このメモと同様に:

note: no known conversion for argument 1 from ‘boost::shared_ptr<apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TTransport> >’ to ‘apache::thrift::protocol::TProtocol*

これらのことが違いを生む場合、私はApache Thrift 1.0-dev、C++ 98を使用しています。

4

2 に答える 2

2

ThriftObject.write() 関数は型の引数を期待しています

apache::thrift::protocol::TProtocol*

つまり、TProtocol 型の「Raw」ポインタです。ここで使用される protocolOut オブジェクトは、タイプの共有ポインターです。

shared_ptr<TBinaryProtocol> protocolOut

shared_ptr の「get()」メソッドは、shared_ptr オブジェクトによって管理されている生のポインターを提供します。したがって、

obj.write(protocolOut.get());

問題を解決する必要があります

于 2015-01-30T06:39:35.927 に答える
1

C++ では、TFileTransport と任意の Thrift プロトコル、つまり TBinaryProtocol を使用できます。

shared_ptr<TFileTransport> transport(new TFileTransport(filename));
shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
yourObj.write(protocol.get()); // to write

また

yourObj.read(protocol.get()); // to read

ファイルが存在することを確認するには、前に開くを使用できます。

open(filename.c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0666); // create file

ps .: 実際には、他のすべてのターゲット言語で非常によく似ています。

于 2014-07-26T14:02:51.567 に答える