views:

126

answers:

3

I'm getting an error with serializing a char* string error C2228: left of '.serialize' must have class/struct/union I could use a std::string and then get a const char* from it. but I require the char* string.

+3  A: 

The error message says it all, there's no support in boost serialization to serialize pointers to primitive types.

You can do something like this in the store code:

int len = strlen(string) + 1;
ar & len;
ar & boost::serialization::make_binary_object(string, len);

and in the load code:

int len;
ar & len;
string = new char[len]; //Don't forget to deallocate the old string
ar & boost::serialization::make_binary_object(string, len);
Andreas Brinck
A: 

There is no way to serialize pointer to something in boost::serialization (I suspect, there is no actual way to do that too). Pointer is just a memory address, these memory addresses are generally specific for instance of object, and, what's really important, this address doesn't contain information where to stop the serialization.

You can't just say to your serializer: "Hey, take something out from this pointer and serialize this something. I don't care what size does it have, just do it..."

First and the optimal solution for your problem is wrapping your char* using std::string or your own string implementation. The second would mean writing special serializing routine for char* and, I suspect, will generally do the same as the first method does.

Kotti
Actually serializing pointers to classes/structs works just fine (as long as you've declared a `serialize` function for it).
Andreas Brinck
+1  A: 

Try this:

struct Example
{
  int i;
  char c;
  char * text; // Prefer std::string to char *

  void Serialize(std::ostream& output)
  {
     output << i << "\n";
     output << c << "\n";

     // Output the length of the text member,
     // followed by the actual text.
     size_t  text_length = 0;
     if (text)
     (
         text_length = strlen(text);
     }
     output << text_length << "\n";
     output << text << "\n";
  };

  void Input(std::istream& input)
  {
     input >> i;
     input.ignore(1000, '\n'); // Eat any characters after the integer.
     input >> c;
     input.ignore(1000, '\n');

     // Read the size of the text data.
     size_t  text_length = 0;
     input >> text_length;
     input.ignore(1000, '\n');
     delete[] text; // Destroy previous contents, if any.
     text = NULL;
     if (text_length)
     {
         text = new char[text_length];
         input.read(text, text_length);
     }
};

Since pointers are not portable, the data must be written instead.

The text is known as a variable length field. Variable length fields are commonly output (serialized) in two data structures: length followed by data OR data followed by terminal character. Specifying the length first allows usage of block reading. With the latter data structure, the data must be read one unit at a time until the terminal character is read. Note: the latter data structure also implies that the terminal character cannot be part of the set of data items.

Some important issue to think about for serialization:
1. Use a format that is platform independent, such as ASCII text for numbers.
2. If a platform method is not available or allowed, define the exact specification for numbers, including Endianness and maximum length.
3. For floating point numbers, the specification should treat the components of a floating point number as individual numbers that have to abide by the specification for a number (i.e. exponent, magnitude and mantissa).
4. Prefer fixed length records to variable length records.
5. Prefer serializing to a buffer. Users of the object can then create a buffer of one or more objects and write the buffer as one block (using one operation). Likewise for input.
6. Prefer using a database to serializing. Although this may not be possible for networking, try every effort to have a database manage the data. The database may be able to send the data over the network.

Thomas Matthews
+1: saves the overhead of using boost with a struct/class containing non-struct/class type data.
Dave18