left Operand must be l-value

This topic is 2804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 on other sites
Can you show some actual code? if(Objects[x]==0) does not really qualify for requiring l-valueness.

Share on other sites
Fairly certain you need to go like this:

if((*Objects)[x] == 0)
//do this

Share on other sites
Quote:
 Original post by CRYP7IKFairly 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) {} // &lt;-- accidental assignment, still valid, but g++ gives a warning //if (0 = foos[0]) {} // &lt;-- 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 on other sites
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 on other sites

Here is some code

CXFile** Objects;
Objects = new CXFile* [ObjectNums];
for(int i =0; i< ObjectNums; i++){
Objects = 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 on other sites

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

Isn't this backward?

Share on other sites
Yeah, it is wrong, but you got the idea from my mistake.. :P

Share on other sites
I got it working, thanks for the help.

Share on other sites
Quote:
 Original post by KryogenikI got it working, thanks for the help.

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• Forum Statistics

• Total Topics
631753
• Total Posts
3002098
×