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.
left Operand must be l-value
Can you show some actual code? if(Objects[x]==0) does not really qualify for requiring l-valueness.
Quote:Original post by CRYP7IK
Fairly certain you need to go like this:
*** Source Snippet Removed ***
Negative, unfortunately. Testcase:
$ cat test.ccint 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.cctest.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 :)
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.
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.
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 zeroObjects[5] = new CXFile();// allocate memory and place the point into array pos 5// now you can test like thisif(Objects[5] ==0) cout<<"Slot 5 has intialized memory ooh boy!"<<endl;else cout<<"Slot 5 is empty"<<endl;
if(Objects[5] ==0) cout<<"Slot 5 has intialized memory ooh boy!"<<endl;else cout<<"Slot 5 is empty"<<endl;
Isn't this backward?
Quote:Original post by Kryogenik
I got it working, thanks for the help.
You can help this thread by posting your solution.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement