I have a vector of a class of units: std::vector<Unit> Units; //list of units
In Unit the constructor Unit::Unit() has a boolean value 'selected'. calling .select sets it to true
but for some reason when i go through each unit in the vector:
for(int i=0;i < Units.size(); i++){
Units.at(i).select(driver,images);
}
it resets all values as if it got created again, with selected turning false. but unit.select();
select does change inside the function, but not in the class. why would it do that?
Name:
Anonymous2007-07-05 4:11 ID:tFTOqSVZ
Sorry, we only discuss Satori/Lisp here. Take your C++ fagottry back to /comp/
Name:
Anonymous2007-07-05 4:14 ID:gbn8N62F
Bah, i solved it. not worth time writing.
Those interested it was because in my for loop of selecting:
for(int i=0;i < Units.size(); i++){
Unit strd = Units.at(i);
if(strd.hitTest((unsigned int)dragStartX,(unsigned int)dragStartY,(unsigned int)dragToX,(unsigned int)dragToY)){
strd .select();
}else{
strd .unselect();
}
}
created a new pointer to the object, thus treating it like a new class.
changing strd to Units.at(i) made it point to the unit itself, not the the new pointer
>>4
I disagree. So far, 11/18 threads on the front page are Satori or Satori related. And that's only in the first day. I expect it will continue to grow as more people begin to open their minds.
Name:
Anonymous2007-07-05 4:32 ID:vqWhloyk
>>3
Learn the difference between classes and objects.
I don't see any pointers (or references) there, you are copying objects.
Consider adding a copy constructor declaration in private. That should prevent some of those accidents.
Name:
Anonymous2007-07-05 4:46 ID:gxj7JQzM
std::vector<Unit *> Units;
fixed
Name:
Anonymous2007-07-05 5:27 ID:xc/kql5U
>>3
There are no pointers in there. What you did was create a new Unit instance called strd, then copy-assigned the one in the vector to strd. That's operator= for you.
Also, why the explicit index?
for (std::vector<Unit>::iterator u = Units.begin(); u != Units.end(); ++u) {
if (u->hitTest(dragStartX, dragStartY, dragToX, dragToY)) {
u->select();
} else {
u->unselect();
}
}
You wouldn't have to second-guess yourself with that bounds-checking at() method, plus it's faster because you're not copying your Unit instances unnecessarily.
Name:
Anonymous2007-07-05 9:51 ID:gbn8N62F
...
Name:
Anonymous2007-07-05 9:54 ID:gbn8N62F
>>7
cannot convert parameter 1 from 'class Unit' to 'class Unit *const & '