cateof cateof - 5 months ago 29
C++ Question

error: lvalue required as unary ‘&’ operand when passing std::string

I am writing C++ code and I am using a struct.

struct Info_t {
cass_int64_t internal_id;
cass_double_t amount;
std::string client;

I am passing a pointer to this struct in a function and I am using it like this

get_string(row_by_name(row, "cl"), &info->client.c_str());

but the compiler complains with error "*error: lvalue required as unary ‘&’ operand"

The third argument of the row_by_name should be const char* and I am passing a std::string that I am trying to make it char* with c_str(). What is wrong?


To escape the error just write

get_string(row_by_name(row, "cl"), info->client.c_str());

You may not apply operator & to temporary objects. This expression


already has type const char *.

However it is not clear what you are trying to achieve. Maybe you should declare the corresponding parameter as for example const std::string & and pass as the argument the object of std::string itself?

If the corresponding parameter is declared like const char ** though that is strange, then before passing the argument you have to declare an intermediate variable for example like this

const char *p = info->client.c_str();

and pass it as argument using expression &p