Vector problems in C++ class

Started by
0 comments, last by Paradigm Shifter 11 years ago

I have been having a problem with the vector index , saying that the vector subscript is out of range. I know where the problem in the code is originating, however, I have no idea why or how the index is getting out of range. This is a DarkGDK project which I don't know will make any difference in helping me with it.

Here is the code that I believe is causing it:


		else
		{
			boy.setImageIndex(
				(boy.getImageIndex()+1 % (boy.getVectorSize())));
			car.display(dbMouseX(), dbMouseY());
			boy.follow(car);
		}

There are five indices in the vector from 0 to 4. The getVectorSize function is nothing more than vector.size(). Here is the function getImageIndex code:


int Sprite::getImageIndex() const
{
	if(images.size() != 4)
	return imageIndex;
}

Any help will be greatly appreciated. If you need any other information, please let me know and I will get it.

Advertisement

boy.getImageIndex()+1 % (boy.getVectorSize())

Is the problem, % has higher precendence than +, it needs to be

(boy.getImageIndex()+1) % (boy.getVectorSize())

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

This topic is closed to new replies.

Advertisement