Results 1 to 4 of 4
Okay, so I'm not really a C++ noob at all, but I am a little rusty at the moment. Still, I CANNOT figure out what is wrong with the following ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 03-20-2011 #1Just Joined!
- Join Date
- Dec 2006
- Posts
- 47
[SOLVED] Trouble with out of range vector iterator
Okay, so I'm not really a C++ noob at all, but I am a little rusty at the moment. Still, I CANNOT figure out what is wrong with the following code. I'm using Visual Studio 2010.
I THINK I got all of the pertinent code. Anyway, it fails at the *** line. I CANNOT figure out why. If I modify it to be linein.size()-30, it STILL gives me an error. That makes zero sense.Code:#include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; struct student { string studentID; vector<string> term; vector<vector<string>> courses; }; int main() { vector<student> recordVec; student record; vector<string> termVec; vector<vector<string>> classVec; string studentData; string name1 = "datain"; ifstream infile; infile.open(name1.c_str()); if (infile.fail()) { cout << "Error opening " << name1 << endl; return 1; } string linein; while (getline(infile, linein)) { int j = 0; if (studentData != record.studentID) { // Get class data studentData.clear(); int test = linein.size(); *** for (j; j < linein.size(); ++j) studentData.push_back(linein[j]); classVec[classVec.size()].push_back(studentData); record.courses = classVec; record.term = termVec; recordVec.push_back(record); } } }
Anyone? Thanks . . .
- 03-20-2011 #2
What is the exact error that you are getting?
Also, the line that you mentioned has no vectors in it, so I don't know what you mean by "vector iterator".
- 03-20-2011 #3Just Joined!
- Join Date
- Dec 2006
- Posts
- 47
I guess my use of terms was somewhat flawed. I'm not using an iteration type, it, but the for loop iterates through the string linein. Also, I should not have referred to linein as a vector since it is a string, but it was late, and the error I get on that line is
Assertion error: vector subscript out of range.
I'm paraphrasing because I'm mobile, so I can't quote verbatim. The error is a runtime error, not compile time.
It really doesn't make sense. The line is 90 characters. I set a temp variable to track linen.size(), and as the for loop hits 89, the program fails. if I subtract even 30 from the condition in the for loop, the loop still throws an error. I don't get it.
I'll try to get the exact error when I get home.
- 03-20-2011 #4Just Joined!
- Join Date
- Dec 2006
- Posts
- 47
:P I figured out my problem. I had my breakpoints set wrong, and the error was actually on the next line:
classVec[classVec.size()].push_back(studentData);
Since classVec is a multidimmentional vector, I had to create a temporary vector of strings to push onto classVec. You can't push onto a an internal vector if the parent vector is empty.
Guess I was too tired last night. Stupid mistake. Thanks, though!



