Liju Mathew
Liju Mathew

Reputation: 899

error: non-lvalue in unary `&' in C++

We are using a macro wrapper to Bind Where Parameter function.

#define bindWhereClause(fieldName, fieldDataType, fieldData) _bindWhereClause(fieldName, fieldDataType, sizeof(fieldData), &fieldData)

void _bindWhereClause(const char *name, int dataType, int dataSize, void *data)
{
  // Implementation
}

Database.bindWhereClause( "FIRST_NAME", SQL_VARCHAR, name.getFirstName());

When I tried to call the macro with a function as parameter (as above) I am getting the error message "error: non-lvalue in unary `&'".

I am able to call the macro with normal variables like

Database.bindWhereClause( "FIRST_NAME", SQL_VARCHAR, firstName);

How to resolve this? Do I need to use inline functions instead of macro?

Appreciate your help in advance.

Thanks, Mathew Liju

Upvotes: 2

Views: 2809

Answers (4)

Loki Astari
Loki Astari

Reputation: 264621

Why not write a wrapper class around Database.

Check out the Decorator pattern: http://en.wikipedia.org/wiki/Decorator_pattern

Upvotes: 0

caf
caf

Reputation: 239201

You can't take the address of the return value of a function - it is ephemeral.

You need to use a real variable:

Nametype first_name = name.getFirstName();
Database.bindWhereClause( "FIRST_NAME", SQL_VARCHAR, first_name);

// ... and maybe name.setFirstName(first_name); here

Using an inline function would make it compile but it is unlikely to actually work. Presumably this is followed by something like:

Database.execute();

...which expects the objects whose addresses you passed earlier to still be valid. If you use an inline function instead of a macro, those objects will no longer exist, since they were just local to the inline function which has already exited.

Upvotes: 4

Chuck
Chuck

Reputation: 237100

The macro expands to:

_bindWhereClause("FIRST_NAME", SQL_VARCHAR, sizeof(name.getFirstName()), &name.getFirstName())

You can't take the address of the return value of a function, so that's invalid. You can take the address of a variable or argument, so yes, turning it into a function with actual arguments would work.

Upvotes: 1

sharptooth
sharptooth

Reputation: 170509

For this case you will be just fine using an inline function. It is much better in general and you should use inline functions unless using macros is absolutely necessary.

Upvotes: 1

Related Questions