我在嘗試訪問存儲在矢量內的對象時出現了段錯誤。我有一個由過程組成的調查。每個過程都由問題組成。所以Survey Object包含一個vector或Processes,每個process對象包含一個問題向量。類定義如下:訪問對象矢量內的對象時出現分段錯誤
class Survey {
private:
...
vector <Process> survey_processes;
....
public:
......
vector<Process> getSurveyProcesses()
{ return survey_processes; }
void addProcessObj(Process obj)
{ survey_processes.push_back(obj);}
.....
};
class Process
{
private:
....
vector<Question> proc_questions;
....
public:
...
vector<Question> getProcessQuestions()
{ return proc_questions;}
void addQuestionObj(Question obj)
{ proc_questions.push_back(obj); }
.....
};
class Question {
private:
int quesnum;
int answer;
...
public:
Question (int c_ques, int c_ans)
{
quesnum = c_ques;
answer = c_ans;
}
int getQuestionID()
{
return quesnum;
}
int getAnswer()
{
return answer;
}
...
};
作爲創建新過程對象,我將其存儲在過程的矢量和對於每個過程對象,我推問題的向量的問題對象。對於調查對象,我想從流程向量中獲取每個流程,併爲每個流程對象獲取每個問題對象並將其打印出來。
我可以訪問過程對象並使用以下代碼成功打印它們。
cout << ((survey_obj.getSurveyProcesses()).back()).getProcessID() << endl;
當我嘗試從流程向量中提取問題對象時,它給了我一個分割錯誤。我相信我在嘗試訪問對象時發生了一些語法錯誤。我如何訪問嵌入問題矢量內的問題對象,該問題對於給定調查對象的過程對象矢量內的給定過程對象?
以下是發生分段錯誤的代碼的相關部分。
int procnum = 0;
for (unsigned i = 11; i < all_words.size()-1; ++i)
{
vector<string> v;
string s = all_words.at(i);
stringstream ques_stream(s);
int ques_num;
ques_stream >> ques_num;
ques_stream.ignore();
// if process object already exists, do nothing. Otherwise create a new process object and add it to the survey object
if (procnum == ques_num)
;
else
{
Process proc_obj(ques_num);
survey_obj.addProcessObj(proc_obj);
procnum = ques_num;
}
string ques_strng;
ques_stream >> ques_strng;
ques_stream.ignore();
Question ques_obj(ques_strng);
// objective: put the new question object in the question vector of the last process object from the process vector of the survey object
cout << ((survey_obj.getSurveyProcesses()).back()).getProcessID() << endl;
Process current_proc_obj = (survey_obj.getSurveyProcesses()).back();
cout << " Current Process : " << current_proc_obj.getProcessID() << endl;
Question current_question_obj = (current_proc_obj.getProcessQuestions()).back();
((survey_obj.getSurveyProcesses()).back()).addQuestionObj(ques_obj);
**// this is where the segmentation fault occurs when i try to get the last object from process vector of the survey and for that object get the last element of the question vector. print the question id of this question object
cout << " Current Question : " << ((((survey_obj.getSurveyProcesses()).back()).getProcessQuestions()).back()).getQuestionID() << endl;**
cout << " Current Process Question : " << ((current_proc_obj.getProcessQuestions()).back()).getQuestionID() << endl;
}
我試着運行gdb調試器,但它只告訴我,試圖訪問questionID時發生錯誤。我仍然不知道我做錯了什麼,任何幫助將不勝感激。
「Question :: getProcessID」的代碼是什麼? – tmpearce
類問題中沒有ProcessID變量。一個進程有問題的對象,但一個問題對象不存儲它屬於哪個進程對象。到目前爲止,我沒有看到包含它的問題的原因,但是如果我錯過了某些東西,它可以被添加。 – Atif
對不起,我的意思是getQuestionID - 所以我們可以評估它是矢量訪問還是函數調用本身導致崩潰。 – tmpearce