Sign in to follow this  
Kryogenik

left Operand must be l-value

Recommended Posts

Kryogenik    102
So yeah. I have an array that is a pointer to a pointer of my class CXFile, I declared it like this.

CXFile** Objects;

And allocated the array like this (Object Nums equals 2 BTW)

Objects=new CXFile* [ObjectNums];

I can use this array to control classes pointed to by the array, and it's pretty good. The only problem is that I don't know how to check if a certain value in the array has been initialized.

I know what the error means from a google search. It means that the number on the left side of the operator (== in my case) must be a variable or something, like you couldn't do this.

int=32

But I couldn't find anyone who had quite the same problem I'm having on google. I want to see if it is used yet because when you load an object in my little engine, you can specify a spot in the array you want it to be in, or you can set it to -1 and it will find a free spot to put it in for you. But I don't know how to check if a slot is free or not and thats the problem I'm having. I tried to use this to check if it was free, but it doesn't work and gives me the above error.

if(Objects[x]==0)
//Do stuff that would happen if the slot was free

What should I do? In a nutshell, how do I check if a spot in my CXFile** array has been used yet? Thanks for any help.

Share this post


Link to post
Share on other sites
phresnel    953
Quote:
Original post by CRYP7IK
Fairly certain you need to go like this:

*** Source Snippet Removed ***


Negative, unfortunately. Testcase:

$ cat test.cc
int main () {
class Foo{};
const int size = 42;
Foo **foos = new Foo*[size];

if (!foos[0]) {} // valid
if (foos[0] == 0) {} // valid
if (0 == foos[0]) {} // valid
if (foos[0] = 0) {} // <-- accidental assignment, still valid, but g++ gives a warning
//if (0 = foos[0]) {} // <-- accidental assignment to literal, invalid

delete [] foos;
}

$ g++ -Wall -Wextra test.cc
test.cc: In function int main():
test.cc:9:24: Warning: Suggest parentheses around assignment that is to be used as boolean expression
$

[note that I translated the warning from german to english]

The OP misses to tell us some important details :)

Share this post


Link to post
Share on other sites
Kryogenik    102
Well, I don't want to post my whole engine, but this is the important part.

CXFile **Objects;
//CXFile is an abstract class if that is important
Objects=new CXFile* [2];
/*What I don't understand is this, how do I check if I have assigned a value to a certain spot in my array like this*/
if (Objects[1]==0)
//Do stuff that would happen if there is no value
//When I try that if statement, it gives me the error in the title of my thread

So what do I need to do, I tried if((*Objects)[x] == 0) and it gave me this error: binary '==' : no operator found which takes a left-hand operand of type 'CXFile' (or there is no acceptable conversion). And none of the things in phresnel's example work. Maybe there's something different about VC++. Thanks for any help.

Share this post


Link to post
Share on other sites
smasherprog    568


Here is some code


CXFile** Objects;
Objects = new CXFile* [ObjectNums];
for(int i =0; i< ObjectNums; i++){
Objects[i] = 0;
}
// or you can do a memset(Objects, 0, sizeof(Objects*)*ObjectNums);
// now all values in your array are set to zero

Objects[5] = new CXFile();// allocate memory and place the point into array pos 5
// now you can test like this

if(Objects[5] ==0)
cout<<"Slot 5 has intialized memory ooh boy!"<<endl;
else
cout<<"Slot 5 is empty"<<endl;


Share this post


Link to post
Share on other sites
Kryogenik    102
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

Share this post


Link to post
Share on other sites
Kryogenik    102
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[i];Objects[i]=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.

Share this post


Link to post
Share on other sites
phresnel    953
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[i];Objects[i]=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[i];
Objects[i]=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[i]) delete Objects[i];
}
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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this