Archived

This topic is now archived and is closed to further replies.

Squirell

Vector Error?

Recommended Posts

I''ve got this error that I can''t figure out at all. Can anyone help me please? This code is for my game. What I am trying to do is create a new unit. CBuilding calls CUnit''s NewUnit method. NewUnit takes a couple of stats and creates a struct that it pushs into a vector. When I take out the vector.push_back(struct) part it works fine but when it is in the program crashes. Debug mode just freezes my computer. Heres the code:

#define TEAMA 0
#define RIFLEMAN 0

//Called from CBuilding

Unit->NewUnit(TEAMA, RIFLEMAN, 200, 200);


/////////////////////////////////////////

/*CUnit.h*/
struct UNITSTRUCT {
        int Xpos, Ypos;
        int speed;
        RECT ImagePosition;
        RECT SelectImagePosition; //Position of "selected image"

        int health;
        int team; //O is player, 1 is computer

        bool select;
        vector <int> MoveXList;
        vector <int> MoveYList;
        };
    
vector <UNITSTRUCT> ObjectVector;
//////////////////////////////////////////

//////////////////////////////////////////

/*CUnit.cpp*/
void CUnit::NewUnit(int team, int type, int Xpos, int Ypos)
{
    RECT rect, rect2;
    rect.left = 64;
    rect.right = 80;
    rect.top = 0;
    rect.bottom = 16; //Infantry

    
    rect2.left = 64;
    rect2.right = 80;
    rect2.top = 16;
    rect2.bottom = 32;
    
    UNITSTRUCT temp;
    
    temp.Xpos = Xpos;
    temp.Ypos = Ypos;
    temp.ImagePosition = rect;
    temp.SelectImagePosition = rect2;
    temp.speed = 1;
    temp.health = RIFLEMAN_HEALTH;
    temp.team = team;
    temp.select = false;
    
    
    //If i comment out this line it works fine

    ObjectVector.push_back(temp);
}

Share this post


Link to post
Share on other sites
Shallow copies.

std::vector:ush_back makes a copy of the value passed to it. Because your object is non-trivial, the copy constructor invoked does not display the appropriate behavior (a copy constructor is always invoked, regardless of type; it just happens that the copy constructor for simple type is, well, simple). Either implement the copy constructor or maintain a std::vector of pointers or references to CUnit.

If you switch to pointers or references, be aware of the alterations in ownership and memory management semantics.

Share this post


Link to post
Share on other sites
Sorry, but I''m still a newbie so I have no idea how to implement a copy constructor. Does anyone have a site where I can go to figure it out?

Share this post


Link to post
Share on other sites
quote:
Original post by Squirell
Sorry, but I''m still a newbie so I have no idea how to implement a copy constructor. Does anyone have a site where I can go to figure it out?

Actually, I''m not so sure that''s the problem. Oluseyi, it looks like he doesn''t have any pointers to anything, jsut vectors, which should have their copy constructors called by the default copy constructor of his struct, shouldn''t they?

I thikn that made sense. I''m a little tipsy right now.

Share this post


Link to post
Share on other sites
Oluseyi is correct. The absence of pointers or references is the problem. Either the vectors should contain pointers (or references) to UNITSTRUCT or UNITSTRUCT needs a proper copy constructor and assignment operator that calls the std::vector copy constructor.

C-Junkie: The default copy constructor just copies over memory. It doesn't call any constructors for contained classes. That is quite fatal if those classes have dynamic memory allocated.

[edited by - BitMaster on May 21, 2004 11:12:06 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Either the vectors should contain pointers (or references) to UNITSTRUCT or UNITSTRUCT needs a proper copy constructor and assignment operator that calls the std::vector copy constructor.


Any good references on where to learn how to make a proper copy constructor and assignment operator that calls the std::vector copy constructor?

Share this post


Link to post
Share on other sites
MyStruct::MyStruct(const MyStruct& other)
:myInnerObject(other.myInnerObject),...
{

}

MyStruct& MyStruct::operator = (const MyStruct& other)
{
myInnerObject = other.myInnerObject;
...
return *this;
}


That (of course) requires that the inner objects have both a proper copy constructor and assignment operator. That is not true for all classes, but works for std::vector (check documentation).
Furthermore, the complete memory backed by the vector is copied over, which can add quite a lot of overhead if the vector is large. In most cases, it reduces problems to store pointers to structures inside a vector.

[edited by - BitMaster on May 21, 2004 6:02:38 PM]

[edited by - BitMaster on May 21, 2004 6:03:17 PM]

Share this post


Link to post
Share on other sites
Yeah well actually it doenst have anything to do with the copy constructors. This is really stuipd but well.....I forgot to initalise the class so it was a null pointer to unit. Sorry for wasting yalls time.

Share this post


Link to post
Share on other sites
That''s awesome. C-Junkie and I tried to mention that at the very beginning of the thread, then all of you went off on copy c-tors and assignment operators, when he''s storing POD...way to go.

Share this post


Link to post
Share on other sites