[英]Create array of struct instances, then call function inside other struct passing array of objects (pointers)
我已經發布了一個與我在大學考試中所期望的問題類似的問題,現在這是我面臨的另一個具體問題,可能是由於缺乏對指針的關鍵理解。
該問題定義了幾個struct
。
其中之一是struct Question{}
,它具有指針屬性,還有一個數組,用於保存針對該特定問題的所有答案。 在目的地,我應該能夠遍歷所有問題,以便向用戶逐個顯示。
當我實例化考試時(這是對入學考試的模擬),我需要傳遞學生的公民ID號和考試題。
// pi._prijavljeniKandidati[1]->_JMBG is the ID number in question
// 'questions' is supposed to carry all the questions I've hard-coded
// to save myself from entering manually
pi.StartExam(pi._prijavljeniKandidati[1]->_JMBG, questions);
這是我嘗試的方法:
Question* questions = new Question;
// this initializes a single question
// 'answers' is the attribute that is holding all the answers
// the correct answer is BTW determined by an integer that is also
// sent in the below function
char* answers1[4];
answers1[0] = "London";
answers1[1] = "Berlin";
answers1[2] = "Helsinki";
answers1[3] = "Rome";
questions[0].Create("What is the capital of Finland?", answers1, 2);
// another question
char* answers2[3];
answers2[0] = "Ljubljana";
answers2[1] = "Paris";
answers2[2] = "Prague";
questions[0].Create("What is the capital of France?", answers2, 1);
這就是StartExam
函數的實際樣子,盡管這里沒有什么特別之處,但是它顯示了我如何嘗試獲取某些問題的某些值(基於其索引):
// I also tried void PokreniIspit(char* ID, Question* questions[])
void StartExam(char* ID, Question* questions)
{
// this is just some dummy code line, to make sure it works
cout << questions[1]._txtOfQuestion << endl;
}
當我運行應用程序時,控制台崩潰。 有什么明顯的東西會使它崩潰嗎?
為了完整起見,下面是整個Question結構:
// THIS IS HOW I IMAGING THIS STRUCT 'VISUALLY'
//= _txtOfQuestion ["Koji je glavni grad Njemacke?"]
//= _answers[10] //max 10 answers
//==== [0] Peking
//==== [1] London
//==== [2] Berlin
//==== [3] Seattle
//==== [4] Ljubljana
//= _posOfCorrect [2]
//= _points [4]
struct Question{
char* _txtOfQuestion;
char* _answers[4];
int _posOfCorrect;
int _points;
void Unos(char* txt, char* answers[], int posCorrect, int points)
{
_txtOfQuestion= new char[strlen(txt) + 1];
strcpy_s(_txtOfQuestion, strlen(txt) + 1, txt);
for(int i = 0; i < 4; i++){
_answers[i] = new char;
strcpy_s(_answers[i], strlen(_answers[i]) + 1, _answers[i]);
}
_posOfCorrect = posCorrect;
_points = points;
}
謝謝大家的幫助。 發布這個問題並遍歷所有這些代碼,以便從波斯尼亞語翻譯成英語,這使我注意到了哪里出了問題。
另外,由於我最初確實嘗試過像這樣初始化Question
:
Question* questions = new Question[2];
我要重新使用它,因為我確實需要一系列問題。
但是真正的罪魁禍首(並導致控制台損壞)是我在其中包含4
硬編碼的for
循環的事實。
與第一個問題一樣,當我第二個問題包含4個選項/答案時,它就起作用了。
for (int i = 0; i < 4; i++){
_odgovori[i] = new char;
strcpy_s(_odgovori[i], strlen(odgovori[i]) + 1, odgovori[i]);
}
void Unos(const char* txt, const char* answers[], int posCorrect, int points)
{
if (points < 4)
points = 4; //max 4 answer possibilities
_txtOfQuestion= new char[strlen(txt) + 1];
strcpy_s(_txtOfQuestion, strlen(txt) + 1, txt);
for(int i = 0; i < points; i++){
_answers[i] = new char[strlen(answers[i]) + 1];
strcpy_s(_answers[i], strlen(answers[i]) + 1, answers[i]);
}
_posOfCorrect = posCorrect;
_points = points;
}
我改變了一點:
由於您使用字符串文字調用函數,因此此參數的類型應為const char*
。
您應該限制points
參數(如果有人用5個答案調用它會怎樣?)。
for
循環應從0
points
。 如果運行到4並且只有3個答案,那將是不確定的行為。
您需要一個char
數組,而不是1個char
來保存答案。
for
循環中有一些錯別字( answers
與_answers
)。
我建議使用std::string
代替char*
並使用std::vector
或類似的代替數組。
您應該像這樣調用函數:
questions[0].Create("What is the capital of Finland?", answers1, 2, 3);
只有3個答案,因此您將3
作為最后一個參數傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.