lvalue required error

I'm stuck with this simple problem, can't seem to find a work around. Basically I first define a Obj_X and gave it a value of 10. Then I create a int newVal to add 10 to the Obj_X (see below):

#define Obj_X  10
int newVal = Obj_X += 10;

I get the following error: lvalue required as left operand of assignment

Any ideas?

It's just what the error message says 10 is not a variable nor is Obj_X

Mark

Using Purdum's Bucket Analog, the lvalue is where a data bucket resides in memory. The rvalue is what you see when you peek inside the bucket (e.g., the value 10). The data type specifier (e.g., int) tells you how big the bucket is. In your example, you want to assign into an int, so it's a 2-byte bucket.

Now, look at your code;

#define Obj_X  10
int newVal = Obj_X += 10;

All assignment must have an lvalue or the compiler can't find it in memory. After the preprocessor pass, your code looks like:

int newVal = 10 += 10;

Because your #define is a simple textual substitution in the code, there is no variable for "10", hence no lvalue. To fix it, you would need to make a change similar to:

#define Obj_X  10
int newVal = Obj_X;
newVal += 10;

Now there is a lvalue for Obj_X to work with.

Remember too, a #define is very stupid. It is part of the preprocessor. All it does is make a textual substitution. Anywhere that the preprocessor sees "Obj_X" it will replace it with "10". It does not create variables.

econjack: Using Purdum's Bucket Analog, the lvalue is where a data bucket resides in memory. The rvalue is what you see when you peek inside the bucket (e.g., the value 10). The data type specifier (e.g., int) tells you how big the bucket is. In your example, you want to assign into an int, so it's a 2-byte bucket.

Now, look at your code;

#define Obj_X  10
int newVal = Obj_X += 10;

All assignment must have an lvalue or the compiler can't find it in memory. After the preprocessor pass, your code looks like:

int newVal = 10 += 10;

Because your #define is a simple textual substitution in the code, there is no variable for "10", hence no lvalue. To fix it, you would need to make a change similar to:

#define Obj_X  10
int newVal = Obj_X;
newVal += 10;

Now there is a lvalue for Obj_X to work with.

Makes sense. Thanks. :)

KeithRB: Remember too, a #define is very stupid. It is part of the preprocessor. All it does is make a textual substitution. Anywhere that the preprocessor sees "Obj_X" it will replace it with "10". It does not create variables.

Thanks for the heads up, I wasn't sure how it was being interpreted.