0

I'm coding c++11. In a class A, I have a member variable "member" which is defined as

vector<OtherClass> member; // A's member variable

I would like to serialize A ( so I want to all the data type in OtherClass contained in the vector ) into file in binary format so I defined a function write as follows.

void A::write(ostream& os){ // A's function for outputting A
   os.write(reinterpret_cast<const char*>( &(member.size()) ), sizeof(member.size()));
}

But compiler said lvalue required as unary '&' operand.

How can I fix it without creating a temporary object such as long size=member.size() in the write function?

mallea
  • 534
  • 6
  • 17
  • Please make it clear what the exact error message you're getting. – Tatsuyuki Ishi Mar 14 '17 at 11:31
  • This looks unfortunate but impossible to me to achieve without a local. – Tatsuyuki Ishi Mar 14 '17 at 11:33
  • 1
    What exactly are you trying to write to `os`? – YSC Mar 14 '17 at 11:33
  • Related: https://stackoverflow.com/questions/8763398/why-is-it-illegal-to-take-the-address-of-an-rvalue-temporary – Tatsuyuki Ishi Mar 14 '17 at 11:36
  • You are stuck making a temporary as you need an lvalue. Just bite the bullet and do it. If you have a really cool compiler it could even optimize that away and if not copying a 4/8 bytes is not going to be a big deal. The I/O is so heavy you'll never notice a single copy. – NathanOliver Mar 14 '17 at 11:49
  • `ostream` is for formatting things into a character sequence. Why are you (or anybody for that matter) using it for binary? – Spencer Mar 14 '17 at 12:20

2 Answers2

1

The problem is &(member.size()) - you're not assigning size()'s return value and trying to take the address of it (it may not even have an address).

But no idea why you're calling size here anyway you need the start address of the actual data: member.data() (or if you dont have C++11 &member[0])

virgesmith
  • 762
  • 1
  • 7
  • 18
  • When you `write` a vector you need to write it's `size` and then its `data` otherwise you will not know how much to `read` back in. – NathanOliver Mar 14 '17 at 11:47
  • 1
    @virgesmith The OP wants to serialize vector. Anyway he will need to write both data and size. To write size using `ostream.write` method, one needs to know both address and size of what is being written - the address and size of the `size()` return value. – alexeykuzmin0 Mar 14 '17 at 11:47
  • ostream& write (const char* s, streamsize n);See http://www.cplusplus.com/reference/ostream/ostream/write/ i.e in this case `os.write(reinterpret_cast(member.data()), member.size() * sizeof(OtherClass))`. – virgesmith Mar 14 '17 at 11:48
  • @virgesmith As you have lower rep, you will not be able to see that I already deleted the same answer, based on a similar comment from alexeykuzmin0; it seems that in this case, the asker is actually first trying to serialize the *size* of the vector (and will presumably serialize the contents next). – BoBTFish Mar 14 '17 at 11:52
  • don't forget the dubious `sizeof(member.size())` ;) – YSC Mar 14 '17 at 12:27
1

"How can I fix it without creating a temporary object"

You can't. You want to use &, which takes the address of an object. No object, no address. It's really that simple.

Note that your long size = member.size() idea doesn't create a temporary variable. That's an ordinary variable.

MSalters
  • 173,980
  • 10
  • 155
  • 350