mallea
mallea

Reputation: 564

lvalue required as unary '&' operand in c++11

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?

Upvotes: 0

Views: 623

Answers (2)

MSalters
MSalters

Reputation: 179991

"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.

Upvotes: 1

virgesmith
virgesmith

Reputation: 822

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])

Upvotes: 1

Related Questions