I am getting the following error message:

terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
Aborted

It appears that I'm trying to read past the end of a vector somewhere in my code. The error message doesn't give me a whole lot of information about where the problem occurred and checking all of the vectors in my code manually is very tedious.

Is there a quick way to identify which vector caused this problem and which line in the code it occurred?

There are several ways to do this: Everyone prefers something slightly different, and a fair bit depends on your environment, Linux, windows, embedded etc. But here are a few.

First : Debugger route.
The quickest way is to add a try { } catch(...) { // stuff here } around all the code in main(). That way you are guaranteed to get to stuff here.
Then run the code in your debugger, and put a break point at stuff here and back-track from there.

Better/Some debuggers allow you to put a break point on the exception being thrown, but depending on your code you might not want that, [i.e. if you handle a number of exception in other parts of the code and that is normal operation].

Option 2:

Valgrind is a superb (IMHO) memory leak checker, run the code with that and it will tell you were the error was, and maybe about other errors in the code. If you are on Windows, I am assuming there is something equivalent but you will have to google it.

Option 3 : Catch the exception and print out as many of the state variables as possible [e.g. outer loop counters, number of events etc] and try to find out when it goes wrong.

Does that help?

I tried using Valgrind but it was not able to point me to where the error was. I am now going to try using a debugger like gdb. Do you know how to put a break point on the exception being thrown?

Otherwise, I may just have to manually print out variables until I find the problem...

It feels like there should be an easier way because the error is due to reading past the end of a vector so there should be a way to find out which vector caused the crash.

If you are using gdb, then carry out the first operation I suggested. Then you have successfully trapped the exception. All you need to do is type up after putting a breakpoint in the global catch and you are looking at the line that caused the exception.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.