简体   繁体   English

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

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

I've already posted a question in relation to a problem similar to what I can expect on my college exam and now this is another concrete issue I'm facing, probably due to lacking crucial understanding of pointers. 我已经发布了一个与我在大学考试中所期望的问题类似的问题,现在这是我面临的另一个具体问题,可能是由于缺乏对指针的关键理解。

The problem has several struct s defined. 该问题定义了几个struct

One of them is struct Question{} , which has pointer attributes, and also an array which is to hold all the answers given for that particular question. 其中之一是struct Question{} ,它具有指针属性,还有一个数组,用于保存针对该特定问题的所有答案。 At the destination, I should be able to iterate over all the questions in order to display them one by one to the user. 在目的地,我应该能够遍历所有问题,以便向用户逐个显示。

When I'm instantiating the exam (this is a simulation of an admission exam), I need to pass the student's citizen ID number, and the exam Questions. 当我实例化考试时(这是对入学考试的模拟),我需要传递学生的公民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);

This is how I tried it: 这是我尝试的方法:

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);

And this is how the StartExam function actually looks like, nothing special here though, except that it shows how I tried getting some values of certain questions (based on the index thereof): 这就是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;
}

When I run the app, the console crashes. 当我运行应用程序时,控制台崩溃。 Is there anything obvious that would make it crash? 有什么明显的东西会使它崩溃吗?

For the sake of completeness, here's the whole Question structure: 为了完整起见,下面是整个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;

}

Thank you all for trying to help. 谢谢大家的帮助。 Posting this question and going through all this code in order to translate from Bosnian to English helped me notice what was wrong. 发布这个问题并遍历所有这些代码,以便从波斯尼亚语翻译成英语,这使我注意到了哪里出了问题。

Also, since I did initially try initializing Question like this: 另外,由于我最初确实尝试过像这样初始化Question

Question* questions = new Question[2];

I'm back to using that because I do need array of questions. 我要重新使用它,因为我确实需要一系列问题。

But the real culprit (and cause for console to break) was the fact I had this for loop with the 4 hardcoded in it. 但是真正的罪魁祸首(并导致控制台损坏)是我在其中包含4硬编码的for循环的事实。

As soon as I made my second question contain 4 options/answers, just like the first one - it worked. 与第一个问题一样,当我第二个问题包含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;
}

I changed a little bit: 我改变了一点:

Since you call the function with string literals, the type of this parameters should be const char* . 由于您使用字符串文字调用函数,因此此参数的类型应为const char*

You should limit the points parameter (What happens if someone call it with 5 answers?). 您应该限制points参数(如果有人用5个答案调用它会怎样?)。

The for loop should run from 0 to points . for循环应从0 points It would be undefined behaviour if it runs till 4 and there are only 3 answers. 如果运行到4并且只有3个答案,那将是不确定的行为。

You need an array of char s, not only 1 char to save the answer. 您需要一个char数组,而不是1个char来保存答案。

There were some typos in the for loop ( answers is not the same as _answers ). for循环中有一些错别字( answers_answers )。

I suggest to use std::string instead of char* and a std::vector or similar instead of the array. 我建议使用std::string代替char*并使用std::vector或类似的代替数组。

You should call the function like: 您应该像这样调用函数:

questions[0].Create("What is the capital of Finland?", answers1, 2, 3);

there are only 3 answers, so you pass a 3 as last parameter. 只有3个答案,因此您将3作为最后一个参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM