#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    UK
    Posts
    41
    Rep Power
    11

    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.

    Code:
    // The structs I'm playing with
    struct CoOrd
    {
       int x, y;
    };
    
    struct Thing
    {
       int x, y;
    };
    
    
    
    
    // The vectors, members of class MapTSP
       vector<CoOrd>  m_vecCityCoOrds;
       vector<Thing>  Test;
    
    
    
    
    // The routine that adds the objects to the vectors, from a member function of MapTSP
    
    CoOrd ThisCity;
    Thing ThisThing;
    while (angle < 2 * pi)
    {
    
    
         m_vecCityCoOrds.push_back(ThisCity);
    
        //      Test.push_back(ThisThing);
    
        angle += SegmentSize;
    }
    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.

    Any ideas of what might be going on?

    Thanks!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    1
    Rep Power
    0
    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:
    vector<CoOrd> *m_vecCityCoOrds;

    and in the constructor:
    this->m_vecCityCoOrds = new vector<CoOrd>;

    Now you can use it like that:
    m_vecCityCoOrds->push_back(ThisCity);

    And to access it:
    m_vecCityCoOrds->at(0);
    or
    (*m_vecCityCoOrds)[0]; //This one looks better :)

IMN logo majestic logo threadwatch logo seochat tools logo