July 22nd, 2003, 04:06 PM
Problem containing vector
I'm creating a genetic algorithm program and I have classes containing STL vectors. One of them is giving me pains and I don't know why.
The code is from the book "AI techniques for Game Programming", one of the game programming series edited by Andre LaMothe. My version of the code is not exactly like the code in the book, but I don't think that my changes should affect this particular situation.
The trouble is that when I try to insert objects of the "CoOrd" structure into a vector, the program crashes. It compiles fine, but when launched, it fails at the point of initialising an object of the class MapTSP, that contains the troublesome vector. The MapTSP object is initialised in the constructor of the CgaTSP class, which is initialised at startup.
Now, I know that I must be doing something stupid, but, gad dang it, I just can't figure out what it is. I have reduced the CoOrd structure to just two ints, x and y. Meanwhile I have also made a struct named "Thing" that is identical to CoOrd (int x, y). I have made a vector to contain Things and made it a member of the MapTSP class along with the vector containing CoOrds.
I can insert Things into the vector<thing> in exactly the same way I'm trying to insert CoOrds, and the program runs fine! Yet when I try to insert CoOrds into the vector<CoOrd> the program crashes!
Following is code.
I've taken out everything except the lines that push back the objects into the vectors. The code you see here equals *crash*. But if I comment out the m_vecCityCoOrds line and uncomment the Test line, it works smooth and dandy.
// The structs I'm playing with
int x, y;
int x, y;
// The vectors, members of class MapTSP
// The routine that adds the objects to the vectors, from a member function of MapTSP
while (angle < 2 * pi)
angle += SegmentSize;
Any ideas of what might be going on?
August 23rd, 2003, 09:37 PM
I've had the same problem about a week ago. The solution is to declare an vector pointer, because a pointer has a constant length. All the members of a class must have a constant length!
declare like that:
and in the constructor:
this->m_vecCityCoOrds = new vector<CoOrd>;
Now you can use it like that:
And to access it:
(*m_vecCityCoOrds); //This one looks better :)