Tellrell White
Tellrell White

Reputation: 85

Why is ParseFromString() not producing an output in my application using protobuf?

I currently have a *.cpp file where I import the needed *.proto.h file and instantiate an object based on one of the messages(Heartbeat) from my *.proto file. My goal is to use the set_value(), SerializeToString(), and ParseFromString().I've had success accomplishing the first two tasks. When using ParseFromString() not output is produced to the screen. I'm pretty new to protobufs so there may be something I'm overlooking. This is my code.

#include <iostream>
#include <fstream>
#include <string>
#include "cpnt.pb.h"

using namespace std;

int main()
{
    wombat::HeartBeat h;

    string m;
    string t;
    string v;

    h.set_value(1);
    v = h.SerializeToString(&m);
    t  = h.ParseFromString(v);

    cout << "\n The message received is:" << h.value();
    cout << "\n The binary data stored in v is:" << v;
    cout << "\n The parsed data stored in t is:" << t <<::endl;
    return 0;

}

And this is a printout of the output:

enter image description here

Upvotes: 0

Views: 777

Answers (1)

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122726

You misunderstood how SerializeToString works. h.SerializeToString(&m); serializes h and stores the result in m. h.ParseFromString(v); parsed from the string v to set members of h. Hence it should be

h.set_value(1);
h.SerializeToString(&m);
h.ParseFromString(m);

Both functions retun a bool, but I admit that I didn't find what it is good for in the documentation. Probably to signal an error.

Upvotes: 1

Related Questions