left Operand must be l-value

Started by
11 comments, last by phresnel 13 years, 5 months ago
I set all the values in the array to zero and it worked. Also, in this if statement

if (Objects[x]==0)

I also had this in it

if (Objects[x]==0 && done=0)

I forgot the extra equal sign next to done, so that was probably a problem as well. Kind of a dumb mistake XD
Advertisement
Sorry to double post, but I ran into another problem with this array (so much for getting it working XD). How do I delete all the class objects and the array?
I thought it would be like this:

for (int i=0;i<ObjectNum;i++)
delete Objects;Objects=0;

Or this:

delete [] Objects;Objects=0;

Or both of them, but I've tried both of them seperately and together, and I get memory leaks. I checked for them by putting this at the top of the WinMain

#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

So anyway, how do I delete all the class objects and the array? The array is a pointer to a pointer of an abstract class, it looks like this

CXFile** Objects;
Objects=new CXFile* [2];

And the pointers point to pointers of objects of either my CXFileStatic class or my CXFileEntity class, not CXFile. Does that cause problems when calling delete? Thanks for any help.
Quote:Original post by Kryogenik
I set all the values in the array to zero and it worked. Also, in this if statement

if (Objects[x]==0)

I also had this in it

if (Objects[x]==0 && done=0)

I forgot the extra equal sign next to done, so that was probably a problem as well. Kind of a dumb mistake XD


That is why we ask to post actual code, not code that you think is wrong. if (Objects[x]==0) is perfectly valid, whereas done=0 is not necessarily valid.

for (int i=0;i<ObjectNum;i++)delete Objects;Objects=0;

This is prolly not valid C++ and definitely not what you want. Let me enlighten you:

for (int i=0;i<ObjectNum;i++)    delete Objects;Objects=0;


In case you have no variable i declared above the for loop, this will luckily not compile. You need braces if you have more than statement in the for-loop.

Here is the proper way to delete the array:

for (int i=0;i<ObjectNum;i++) {    if (Objects) delete Objects;}delete [] Objects;


Rules-of-thumb:
  • Use free() after malloc().

  • Use delete after new.

  • Use delete [] after new [].


Also, have you heard of boost::shared_ptr?

This topic is closed to new replies.

Advertisement