Aug 26, 2010 at 2:36pm UTC
An object of type std::string
is to call its size()
method and reference the returned variable:
&strIn.size()
or
&(strIn.size())
Why does this code produce this error:
error: lvalue required as unary '&' operand
size()
should return a variable of type size_t
, which I would think should be able to be referenced like any other variable.
Last edited on Aug 26, 2010 at 2:37pm UTC
Aug 26, 2010 at 2:46pm UTC
No it can't. You would only be able to do that if the size() member function returned a reference to it's size member variable, but it only returns a copy.
The new C++0x standard will ratify this, however.
Last edited on Aug 26, 2010 at 2:47pm UTC
Aug 26, 2010 at 2:48pm UTC
size_t is equivalent to a number (I can't remember if it was an unsigned int). So, you're taking a reference of a number, which you can't.
You should just use strIn.size ()
.
Aug 26, 2010 at 8:24pm UTC
You could just use streams instead of the C library. It would be as simple as ofs << strIn.size();
Aug 26, 2010 at 10:26pm UTC
except that outputs it as text, and not as binary.
The iostream version of outputting binary also requires a temp var.