Public Group

# Vector problems in C++ class

This topic is 1927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

Edited by manningb57

##### Share on other sites

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

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

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

1. 1
Rutin
21
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×