-
Pointer Problems
Hi,
I have a problem with a certain part of code I'm currently working on.
I have a vector sorting a set of HObjects (self defined class).
I want to create a pointer to a HObject being popped from the vector.
Code:
vector<HObjects> set;
fillvector(set);
HObject *new;
new = set.pop_back(); //***
However, (***) is giving me this error:
"void value not ignored as it ought to be"
Is there something wrong with the way I'm doing this?
Thank You.
-
pop_back does not return a value for one, you have to get the values beforehand. Also, you are popping it so the object should be lost, the pointer would mean nothing. You should probably be storing a vector of pointers instead. Something like this:
Code:
vector<HObjects*> set;
fillvector(set); //puts dyn allocated instances in it
HObject *new;
new = set[set.size()-1];
set.pop_back();
try that out
-
just realized, you used "new" as a variable name too. naughty naughty.
-
Yes, new is a reserved keyword. Your IDE should have changed its color for you. (Assuming you use that feature) As Hershlag said, you cannot use reserved keywords for variable names.