Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Constructor called while accessing vector

Name: Anonymous 2007-07-05 4:10 ID:gbn8N62F

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: Anonymous 2007-07-05 4:11 ID:tFTOqSVZ

Sorry, we only discuss Satori/Lisp here. Take your C++ fagottry back to /comp/

Name: Anonymous 2007-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

Name: Anonymous 2007-07-05 4:15 ID:gbn8N62F

>>2
that lame meme wont go far. gb2/tryhard/

Name: Anonymous 2007-07-05 4:30 ID:tFTOqSVZ

>>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: Anonymous 2007-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: Anonymous 2007-07-05 4:46 ID:gxj7JQzM

std::vector<Unit *> Units;

fixed

Name: Anonymous 2007-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: Anonymous 2007-07-05 9:51 ID:gbn8N62F

...

Name: Anonymous 2007-07-05 9:54 ID:gbn8N62F

>>7
cannot convert parameter 1 from 'class Unit' to 'class Unit *const & '

Name: Anonymous 2007-07-05 9:55 ID:gbn8N62F

>>8
Ahhhh, thax heaps =D

[spoiler][b]EXPERT PROGRAMMER =D[/b][/spoiler]

Name: Anonymous 2007-07-05 10:29 ID:tHzlnT1y

not Unit strd = Units.at(i);
Unit& strd = Units.at(i);

WIN

Name: Anonymous 2007-07-05 10:57 ID:Heaven

>>11
GET THE FUCK OUT

Name: Anonymous 2011-11-10 11:23

old thread is old

Name: Anonymous 2011-11-10 11:23

old thread is old

Don't change these.
Name: Email:
Entire Thread Thread List