创建结构实例数组,然后调用其他结构内部的函数,传递对象数组(指针)
Create array of struct instances, then call function inside other struct passing array of objects (pointers)
我已经发布了一个与我在大学考试中所期望的问题类似的问题,现在这是我面临的另一个具体问题,可能是因为缺乏对指针的关键理解。
该问题定义了几个struct
。
其中一个是struct Question{}
,它具有指针属性,还有一个数组,用于保存该特定问题的所有答案。在目的地,我应该能够迭代所有问题,以便将它们逐一显示给用户。
当我实例化考试(这是入学考试的模拟(时,我需要通过学生的公民身份证号码和考试问题。
// 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;
}
当我运行应用程序时,控制台会崩溃。有什么明显的东西会让它崩溃吗?
为了完整起见,以下是整个问题结构:
// 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;
}
感谢大家的帮助。发布这个问题并浏览所有这些代码,以便从波斯尼亚语翻译成英语,帮助我注意到了问题所在。
此外,由于I确实最初尝试初始化Question
,如下所示:
Question* questions = new Question[2];
我又开始使用它了,因为我确实需要一系列的问题。
但真正的罪魁祸首(也是控制台崩溃的原因(是我有一个for
循环,里面硬编码了4
当我让我的第二个问题包含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
作为最后一个参数。
- 从函数中全局删除并重新实例化数组结构,而无需在编译时知道数组的大小
- 如何使用函数的输出初始化 const 数组结构字段?
- 传递数组结构、ofstream 和 interger 以运行
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 将文本文件读取到数组结构中
- C/C++中数组结构和数组结构的通用接口
- C++ MDC final-在字符类型的数组结构中按字母顺序对记录中的名称进行排序
- C++ 使用数组结构创建平衡的二叉搜索树
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- 使用 vector 在 c++ 中声明 3D 数组结构
- 数组结构无法正确打印
- 如何在C++中访问数组结构内部的数组结构
- CIN进入数组结构似乎什么也没输入
- ifstream将数组结构到txt文件中,然后尽可能将其提取为数组
- wlanapi-将WlanFreeMemory释放其WLAN_INTERFACE_INFO数组结构
- 显示数组结构 c++
- C++ 使用函数访问数组结构的方法是什么?
- 将庞大的数组结构复制到 GPU
- 如何封送包含字符矩阵的数组结构
- C++多维数组结构的对齐