• Advertisement
Sign in to follow this  

Why does this vector keep registering errors?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my code, I have a CCharacter class, which is used in both the Main Character's pointer and a vector, which contains all the enemies in the map.
When I'm doing calculations with the arrays, I boiled one specific function down to the following:

if(enemies[0]->GetHP() <= 0)
enemies.erase(enemies.begin() + 0);

I know it's not efficient at all, but I'm first trying to get it to work at all, even if it just deletes the first enemy.
However, later in the function, I do a little calculation with all the enemies. I wanted to avoid errors, so I made sure that the enemy vector has at least one item before performing functions.

if(enemies.size() > 0)
{
// Calculations...all that jazz
}

That line (if there are enemies) works fine, and goes through the calculation. However, if there are no enemies, it crashes the program, with the following error message:
"Debug Assertion Failed!
...
Expression: vector subscript out of range"

Is there any way to find out if a vector has members without getting that error message?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
However, later in the function, I do a little calculation with all the enemies. I wanted to avoid errors, so I made sure that the enemy vector has at least one item before performing functions.

if(enemies.size() > 0)
{
// Calculations...all that jazz
}

That line (if there are enemies) works fine, and goes through the calculation. However, if there are no enemies, it crashes the program, with the following error message:
"Debug Assertion Failed!
...
Expression: vector subscript out of range"

Is there any way to find out if a vector has members without getting that error message?
First of all, you can write:
if (!enemies.empty())
Instead, which is a little more idiomatic.

As for the error you're seeing, I think your best bet would be to step through the program in the debugger to determine exactly what line is triggering the assertion, what index is being used, and what the size of the array is at that point.

There are probably some other stylistic and technical improvements that can be made here, but fixing the subscript error is probably the first order of business.

Also, I'd recommend dropping the 'C' prefix from your class name(s); in other words, just call the class Character, not CCharacter. (Not all programmers agree on this, but I think the general thinking these days is that type prefixes of this sort aren't particularly beneficial and should usually be avoided.)

Share this post


Link to post
Share on other sites
Quote:
First of all, you can write:
if (!enemies.empty())
Instead, which is a little more idiomatic.
As for the error you're seeing, I think your best bet would be to step through the program in the debugger to determine exactly what line is triggering the assertion, what index is being used, and what the size of the array is at that point.


Ok, I'm not quite the programmer to start making my own naming rules yet :P that's just the way I learned it first...I'll hopefully form my own specific style later.

And wow...turns out that wasn't the problem at all. It seemed like it at first, because I would get the error seemingly instantaneously (thanks to computer speed) to when I told it to run that command.

The actual problem is the code to delete an enemy!

enemies[0]->DrainHP(5);
if(enemies[0]->GetHP() <= 0)
enemies.erase(m_pMonsters.begin() + 0);


That never checks to make sure enemies[0] exists though...oops!

Share this post


Link to post
Share on other sites
Quote:
Ok, I'm not quite the programmer to start making my own naming rules yet :P that's just the way I learned it first...I'll hopefully form my own specific style later.
Although you'll tend to make better choices about naming and style the more experience you have, you don't need to be any particular level of programmer to make decisions about what sort of naming convention is right for you.

Naming conventions are first and foremost to help the programmers who are working with the code. If you can think of ways in which the 'C' prefix helps you, then by all means, use it. But, if you're just using it because you've seen it done that way elsewhere, don't feel like you have to keep it.

Even if you're a relative beginner, you can still choose what naming conventions you do and do not want to use :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement