创建结构实例数组,然后调用其他结构内部的函数,传递对象数组(指针)

Create array of struct instances, then call function inside other struct passing array of objects (pointers)

本文关键字:数组 结构 对象 指针 函数 内部 然后 实例 调用 其他 创建      更新时间:2023-10-16

我已经发布了一个与我在大学考试中所期望的问题类似的问题,现在这是我面临的另一个具体问题,可能是因为缺乏对指针的关键理解。

该问题定义了几个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作为最后一个参数。