Skip to main content
GameDev.net gamedev.net
🔒 Locked

problem with vector

Started by raccoonone Jan 30, 2006 at 7:21 PM 4 replies 4.2k views
Original Post
raccoonone
raccoonone
I'm having a very strange problem with . My program breaks at the following line in
_DEBUG_ERROR("vector iterators incompatible"); //Line 238

typedef vector<DWORD> FrameVector;

class CPlayer
{
public:
	void AddToINV(BaseItem _Item);
	void RemoveFromINV(DWORD ID);
	void SetINV(CPlayer NewINV) {m_INV = NewINV.m_INV;}
	vector<DWORD> GetINV();
	ErrorMessage FindItem(DWORD ID);
	vector<DWORD> FindItems(ItemTypes Type);
	int SetPos(COORD SetTo);
	int SetPos(int x, int y);
	int SetFacing(Dir SetDir = North);
	int ChangeFacing(Turn TurnBy);
	int ChangePos(Dir Move);
	void vChangeCharacter(CPlayer newcharacter);
	COORD GetPos();
	Dir GetFacing();
	DWORD GetTextureID();
	void AddFrame(DWORD TextureID);
	CPlayer();
	DWORD m_ID;
	DWORD m_MapID;
	DWORD m_dwCharacterID;
private:
	PlayerINV m_INV;
	FrameVector::iterator m_CurrentFrame;
	FrameVector m_TextureFrames;
	COORD Pos;
	CDir Facing;
};

//Get currect texture ID, and increment it if there are multiple frames
DWORD CPlayer::GetTextureID()
{
	if(m_CurrentFrame == m_TextureFrames.end()) // <<Error happens on this line
	{
		m_CurrentFrame = m_TextureFrames.begin();
	}
	DWORD temp = *m_CurrentFrame;
	m_CurrentFrame++;
	
	return temp;
}
I don't understand how there can be a type mismatch, since I typedef'ed FrameVector.
Alan Kemp
Alan Kemp
This isn't a type mismatch problem, if it were you would be getting compile time errors.

I would suspect that m_CurrentFrame is getting invalidated somewhere else. There are lots of things that can invalidate an iterator: inserting and erasing from the vector being the two most common.

In this case you should probably just use an int to track the current frame instead of an iterator.

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Fruny
Fruny
It's not a type mismatch: that would produce a compile-time error, while yours seems to be a run-time error. I suspect the iterator does not actually point into the vector. How did you initialise it? Having more context from where it breaks in would be very helpful.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
raccoonone
raccoonone
Here is the area of code in that triggers the breakpoint.
#if _HAS_ITERATOR_DEBUGGING	void _Compat(const _Myt& _Right) const		{	// test for compatible iterator pair		if (this->_Mycont == 0 || this->_Mycont != _Right._Mycont)			{			_DEBUG_ERROR("vector iterators incompatible"); //<<This line is where it breaks			_SCL_SECURE_INVALID_ARGUMENT;			}		} #endif /* _HAS_ITERATOR_DEBUGGING */


The iterator and vector are only accessed in a few places.
CPlayer::CPlayer(){	m_ID = m_MapID = m_dwCharacterID = 0;	Pos.X = Pos.Y = 0;	Facing.Facing = North;	m_CurrentFrame = m_TextureFrames.end(); //Here it's initialized in the constructor}//Add a frame to the player's animationvoid CPlayer::AddFrame(DWORD TextureID){	//add the new frame	m_TextureFrames.push_back(TextureID);	m_CurrentFrame = m_TextureFrames.begin(); //reset current frame}//change animation to a new charactervoid CPlayer::vChangeCharacter(CPlayer newcharacter){	m_TextureFrames = newcharacter.m_TextureFrames;	m_CurrentFrame = m_TextureFrames.begin();}//Get currect texture ID, and increment it if there are multiple framesDWORD CPlayer::GetTextureID(){	if(m_CurrentFrame == m_TextureFrames.end()) //If the current frame pointer is past the end of the vector, start at the beginning again	{		m_CurrentFrame = m_TextureFrames.begin();	}	DWORD temp = *m_CurrentFrame;	m_CurrentFrame++;		return temp;}
Codarki
Codarki
//Get currect texture ID, and increment it if there are multiple framesDWORD CPlayer::GetTextureID(){    if(m_TextureFrames.empty())        return do_error_handling_or_return_some_invalid_id;    if(m_CurrentFrame == m_TextureFrames.end())    {        m_CurrentFrame = m_TextureFrames.begin();    }    DWORD temp = *m_CurrentFrame;    m_CurrentFrame++;    return temp;}


also make sure your CPlayer's copy construction and assignment operator are working properly if they are used.
raccoonone
raccoonone
Ah yes, that was the problem, thanks!

I'm going to write myself a note, saying "check the copy constructor and operator= if something is wrong!", I've had more problems with that than just about anything.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.