简体   繁体   English

C++ 读访问冲突,结构向量

[英]C++ read access violation, vector of structs

I have a "program" and i want to behave the way i would like to, but the situation is:我有一个“程序”,我想按照我想要的方式行事,但情况是:

I want to create a vector of structs with max element equal to 25 and then inicialize every struct member (this time only the name) through a function.我想创建一个最大元素等于 25 的结构向量,然后通过函数初始化每个结构成员(这次只有名称)。 My problem is i get an exception error: ( Exception thrown: read access violation ) and i have no idea what i did wrong.我的问题是我收到一个异常错误:(抛出异常:读取访问冲突),我不知道我做错了什么。 (and the program needs to end at the name input with one "-" character.) (并且程序需要在名称输入处以一个“-”字符结束。)

The code:编码:

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#define MAXstudent 25
#include <sstream> 
#include <vector>
using namespace std;

struct student {
    string name; 
};

void get_input(vector<student>& student_group) {

    for (int i = 0; i < MAXstudent; i++) {

       //name
       string temp_variable = "";
       cout << "Student name: ";
       getline(cin, temp_variable);
       if (temp_variable != "-") {
           //student_group.push_back(student());
           student_group[i].name = temp_variable;
           cout << endl;
       }
       else {
           student_group.push_back(student());
           student_group[i].name = temp_variable;
           break;
       }
   }
}

void show_solution (vector<student>& student_group) {
    int i = 0;
    while (student_group[i].name != "-") {
        cout << "\nSolutions: " << endl;

        cout << endl << i + 1 << '.' << "kert name: " << student_group[i].name;
        i++;
    }

}

int main() {
    srand(time(0));
    vector<student>student_group[MAXstudent];
    get_input(student_group[MAXstudent]);
    show_solution (student_group[MAXstudent]);
}

Your main should be:你的main应该是:

int main() {
    srand(time(0));
    vector<student>student_group(MAXstudent); // create one vector with MAXstudent elements in it
    //vector<student>student_group[MAXstudent]; - this creates MAXstudent vectors with 0 elements in each
    get_input(student_group);
    show_solution (student_group);
}

In your code you created array of vectors and used a vector with out of bounds of that array.在您的代码中,您创建了向量数组并使用了一个超出该数组边界的向量。 You should understand what you are doing instead of creating random code and expecting it to work someway.您应该了解自己在做什么,而不是创建随机代码并期望它以某种方式工作。

This is what makes the error这就是导致错误的原因

get_input(student_group[MAXstudent]);
show_solution (student_group[MAXstudent]);
vector<student>student_group[MAXstudent];

Change them to将它们更改为

get_input(student_group);
show_solution (student_group);
vector<student>student_group(MAXstudent);

The student_group[MAXstudent] atempts to reach the element of index 25 (ie) the 26th element while your vector is 25 element. student_group[MAXstudent]尝试到达索引25 (即第 26 个元素)的元素,而您的向量是 25 个元素。

Note that the vector is just an object containing your elements (container), so pass it as mentioned and then you can access your elements in the function body by their indecis.请注意,向量只是一个包含您的元素(容器)的对象,因此如上所述传递它,然后您可以通过它们的不确定性来访问函数体中的元素。

I think you need to read more about std::vector s我认为你需要阅读更多关于std::vector的信息

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

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