gcc error: lvalue required as left operand of assignment in Mappy
agent_smith

My game compiles in MSVC and gcc on Windows so I tried to see if it would compile in Linux. I'm running Ubuntu 9.04, gcc 4.3.3, allegro 4.2. The hello world compiled and ran so I confirmed Allegro was working however when compiling my program it came up with 4 errors compiling Mappy.c. I didn't change Mappy or anything just downloaded it and using it as is. The 4 errors I got were:

error: lvalue required as left operand of assignment
error: lvalue required as left operand of assignment
error: lvalue required as left operand of assignment
error: lvalue required as left operand of assignment

#SelectExpand
1i = mapnumblockstr; while (i) { 2error 1--> ((BITMAP *) myblkstrpt->bgoff) = abmTiles[myblkstrpt->bgoff]; 3 if (myblkstrpt->fgoff!=0) 4error 2--> ((BITMAP *) myblkstrpt->fgoff) = abmTiles[myblkstrpt->fgoff]; 5 if (myblkstrpt->fgoff2!=0) 6error 3--> ((BITMAP *) myblkstrpt->fgoff2) = abmTiles[myblkstrpt->fgoff2]; 7 if (myblkstrpt->fgoff3!=0) 8error 4--> ((BITMAP *) myblkstrpt->fgoff3) = abmTiles[myblkstrpt->fgoff3]; 9 myblkstrpt++; i--; 10}

I don't fully understand what the error means; anyone care to help me re-write this so it'll compile or is something more difficult then re-writing these 4 lines of code? Thanks!

anonymous

It seems that the result of a cast (to non-reference type) is not a left-value, i.e something that you can assign to (although some compilers may disagree, particularly MSVC is known for its relaxedness regarding templates and lvalues/rvalues). A possible solution: declare a pointer, so you won't have to cast in the assignment, or cast to reference to pointer?

#SelectExpand
1int main() 2{ 3 int n; 4 void* p; 5 (int*) p = &n; //error 6} 7 8int main() 9{ 10 int n; 11 void* p; 12 int* q = (int*) p; 13 q = &n; 14} 15 16//or cast to reference to pointer 17int main() 18{ 19 int n; 20 void* p; 21 (int*&) p = &n; 22}

Evert

You should cast the rvalue to be of the same type as the lvalue pointer, not the other way around as in that code.

Neil Walker

if you're using C++ then I'd recommend you use the 'combined' playback library (http://www.tilemap.co.uk/mappy.php) as it's C++ and separates the core of mappy from the graphics library. This won't have the issue as it is this now:

myblkstrpt->bgoff = (int) abmTiles[myblkstrpt->bgoff];

So do what Evert says, which is essentially what the above code does.

For anyone's reference,

bgoff is a long int
abmTiles is a BITMAP**

Evert

Hmm... so is that code assuming that a pointer will fit in a long int? In general, that is a bad (unsafe) assumption to make... although it happens to work properly for the time being.

Neil Walker

The newer version has it as just an int, hence the cast in my suggested solution. Whether this (long to int) makes it worse or better, I don't know.

agent_smith

@Neil- are you the same Neil from the tilemap.co.uk forum? I'm using C and not C++... is there any other way?

@anonymous- Thank you I'll try your suggestion

Thank you so much for responding everyone!

Update:
@Neil - is this what you meant?

I changed the 4 errors from this:
((BITMAP *) myblkstrpt->fgoff) = abmTiles[myblkstrpt->fgoff];

To this (respectively):

(myblkstrpt->fgoff) = (int)abmTiles[myblkstrpt->fgoff];

It compiled and the mappy demo worked; however my game did not. I get a segmentation fault. This didn't have in MSVC 9 or gcc 3.4.5 on Windows but I tracked it down to a variable not being initialized (because it was NULL) and then passed to draw_sprite. This has been fixed and now the game works just fine with the exception of not having any sound. The .wav files play just fine in ubuntu but are not playing in allegro. I'll have to research this more and post a question later if I can't find a resolution.

Thanks again for eveyone's suggestions!

Thread #601028. Printed from Allegro.cc