Awful stupid C++ Error

Started by WhiteRose, November 17, 2014, 12:43:32 am

Previous topic - Next topic

WhiteRose

So, in the spirit of full disclosure, this is actually part of a project that I have to do for a class for school. However, I'm hoping that someone with a little more C++ experience than I might be able to help me, because I've gotten pretty stuck.

Essentially, I have a vector, which I'm passing by reference to another object, which is supposed to print out part of the vector. Even when I just try to print the first element in the vector, however, if gives me a "std::out_of_range" error. But when I check the size of the vector right before that, it correctly returns that the size of the vector is two, so there must be an element in the 0th position, right? This code:


if (attributeList.empty()){
out << "Empty!" << endl;
} else {
out << "Not empty!" << endl;
out << "Vector size: " << attributeList.size() << endl;
out << attributeList.at(0) << endl;
}


prints out:


Not empty!
Vector size: 2
A


"A" is the element contained in the 0th slot of the vector, and it is correctly printing out, so why is it throwing an error here?

This isn't really urgent, but it certainly has me pretty baffled. Can anyone think of why this might be happening?

Zexion

Is it at all possible that you have left an old line of code somewhere? I might be missing something dumb, but I don't see where this error could come from if there is an element in the appropriate slot :o

WhiteRose

Quote from: Zexion on November 17, 2014, 01:03:44 am
Is it at all possible that you have left an old line of code somewhere? I might be missing something dumb, but I don't see where this error could come from if there is an element in the appropriate slot :o


It's definitely possible; I've been trying to get this to work all weekend, so it has all sorts of weird stuff in it by now. Haha. There isn't any extra code in this particular method that should affect this vector, however; the code that I posted of where the error appears is copy and pasted straight from the file. I also know that it isn't something after this, as if I put a print statement right after the last line I posted, it never gets reached. It's really weird....

Blizzard

Try this: If you are using Visual Studio, let the error happen. Now pick the "Retry" option from the dialog. If you don't already have "Call stack" visible in one of the bottom tabs, find it and show it through the menu on top. ( I think it is under "View".) Now check the call stack. You can jump to every call location by double clicking it. That way you will find the exact location where the out_of_range exception happened.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

winkio

It is definitely not a problem with the at(0) call, the fact that it returns the 'A' character is proof of that.  It's a long shot, but try storing the values of attributeList.size() and attributeList.at(0) at the beginning of the block (in an int and a char*, or whatever they are), and then substitute those variables into the output statements.  Then you can determine if the problem is with the output code.

Blizzard

Which is why I think that the error must be triggered somewhere else.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

WhiteRose

@winkio: That's a good idea; I'll give that a try and let you know the results.
@Blizz: I'm actually not using Visual Studio - I'm just using a regular text editor and the command line. Maybe that's why I'm struggling so much. Haha. Maybe I'll install Visual Studio to give your suggestion a try and see if that helps track down the root of the problem.

Blizzard

You definitely should. It makes debugging so much easier. Each VS version has an Express Edition which is free for any kind of use. Just download VS 2013 Express for Windows Desktop and have fun. (VS 2013 Express for Windows 8 is for Metro apps, don't use that one.)
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.