有人可以帮助我解决此错误。我整夜都在努力使此代码正常工作,似乎无法弄清楚。
我正在尝试从向量strout中输出单个值,但我不断收到此EXC_BAD_ACCESS错误。我使用中断来查看变量是否正确,以及是否正确,但错误仍然存在。你能帮我吗?
vector<department> depart;
depart.push_back(department());
while(lineNum <= lineCount)
{
lineNum = lineNum;
depart[lineNum].dept = sTemp1;
depart[lineNum].building = sTemp2;
depart[lineNum].budget = sTempToInt;
depart.push_back(department());
lineNum++;
}
vector<department> deptVec;
cout << "Vector output: " << deptVec[0].budget;
}
else
{
sTemp1.erase (remove (sTemp1.begin(), sTemp1.end(), ' '), sTemp1.end()); //clean up
//sTemp2.erase (remove (sTemp2.begin(), sTemp2.end(), ' '), sTemp2.end()); //clean up
sTemp2.erase(find_if(sTemp2.rbegin(), sTemp2.rend(), bind1st(std::not_equal_to<char>(), ' ')).base(), sTemp2.end());
sTemp3.erase (remove (sTemp3.begin(), sTemp3.end(), ' '), sTemp3.end()); //clean up
sTempToInt = stoi(sTemp1);//file is instructor
vector<intructor> intruct;
intruct.push_back(intructor()); //Push back new subject created with default constructor.
for(int lineNum = 0; lineNum>= lineCount; lineNum++) //Will create a set of vectors for each line.
{
intruct[lineNum].id = sTempToInt;
intruct[lineNum].name = sTemp2;
intruct[lineNum].dept = sTemp3;
intruct.push_back(intructor()); //Adds a new element:
}
cout << sTempToInt << "\n" << sTemp2 << "\n" << sTemp3 << "\n";
}
给我错误的部分是
vector<department> deptVec;
cout << "Vector output: " << deptVec[0].budget;
假设输出一个int但通过一个错误。 我检查了Department.budget是否持有int。不知道要尝试什么。
答案 0 :(得分:0)
deptVec向量为空,您正在尝试从中访问元素。尝试在cout之前将值推送到deptVec。
答案 1 :(得分:0)
我解决了这个问题。无论出于什么原因,我都试图在while循环之后创建一个新的向量,而不是使用while循环中使用的向量。
在此之前:
vector<department> deptVec;
cout << "Vector output: " << deptVec[0].budget;
之后(应该是什么):cout << "Vector output: " << depart[0].dept;