Author Topic: Awful stupid C++ Error  (Read 10944 times)

Offline WhiteRose

  • Trying to code in Ruby
  • Moderator
  • Lexima Warrior
  • ***
  • Posts: 2341
  • LV: 127
  • Gender: Female
  • <3
    • View Profile
Awful stupid C++ Error
« on: November 17, 2014, 07:43:32 AM »
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:

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

prints out:

Code: [Select]
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?

Offline Zexion

  • Addicted to LoL
  • Chaos User
  • ******
  • Posts: 1500
  • LV: 69
  • Gender: Male
  • My anaconda don't...jk it does
    • View Profile
Re: Awful stupid C++ Error
« Reply #1 on: November 17, 2014, 08: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

Offline WhiteRose

  • Trying to code in Ruby
  • Moderator
  • Lexima Warrior
  • ***
  • Posts: 2341
  • LV: 127
  • Gender: Female
  • <3
    • View Profile
Re: Awful stupid C++ Error
« Reply #2 on: November 17, 2014, 08:20:11 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....

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19898
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Awful stupid C++ Error
« Reply #3 on: November 17, 2014, 08:31:06 AM »
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      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline winkio

  • Epiq
  • Administrator
  • Guardian of Chaos
  • *****
  • Posts: 4505
  • LV: 199
  • Gender: Male
  • I am lying.
    • View Profile
Re: Awful stupid C++ Error
« Reply #4 on: November 17, 2014, 08:49:30 AM »
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.

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19898
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Awful stupid C++ Error
« Reply #5 on: November 17, 2014, 08:51:03 AM »
Which is why I think that the error must be triggered somewhere else.
Check out Daygames and our games:

King of Booze      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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

Offline WhiteRose

  • Trying to code in Ruby
  • Moderator
  • Lexima Warrior
  • ***
  • Posts: 2341
  • LV: 127
  • Gender: Female
  • <3
    • View Profile
Re: Awful stupid C++ Error
« Reply #6 on: November 17, 2014, 09:23:50 AM »
@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.

Offline Blizzard

  • This sexy
  • Administrator
  • has over 9000 posts
  • *****
  • Posts: 19898
  • LV: 642
  • Gender: Male
  • Magic midgets.
    • View Profile
    • You're already on it. (-_-')
Re: Awful stupid C++ Error
« Reply #7 on: November 17, 2014, 10:13:24 AM »
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      King of Booze: Never Ever      Pet Bots
Drinking Game for Android      Never have I ever for Android      Pet Bots for Android
Drinking Game for iOS      Never have I ever for iOS      Pet Bots for iOS
Drinking Game on Steam


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

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