简体   繁体   English

类vector没有成员

[英]Class vector has no member

I'm collecting names and test scores to populate a vector. 我正在收集名称和测试分数以填充向量。 Both the function and main method can't recognize the struct's members. 函数和main方法都无法识别该结构的成员。 How can I get it to see the members? 我怎样才能看到会员? Or is there a better way to populate a vector of structs with user input using a function? 还是有更好的方法使用功能使用用户输入来填充结构向量?

I've searched other similar posts, but it seems like it's just a simple code error I missed. 我搜索了其他类似的帖子,但似乎只是我错过的一个简单的代码错误。

#include <iostream>
#include <vector>
using namespace std;

const int classSize = 1;

struct StudentType {
    string studentFName;
    string studentLName;
    int testScore;
    char grade;
};

vector<StudentType> collectStudentData(vector<StudentType> students[classSize]) {
    for (int i = 0; i < classSize; i++) {
        cout << "Student " << i << "'s name and test score" << endl;
        cin >> students[i].studentFName >> students[i].studentLName >> students[i].testScore;
    }
    return students[classSize];
};

int main() {
    vector<StudentType> students[classSize] = {};
    students[classSize] = collectStudentData(students);
    cout << students[1].studentFName << students[1].studentLName << students[1].studentFName;
};

'studentFName': is not a member of 'std::vector>' 'studentFName':不是'std :: vector>'的成员

This line creates an array of vectors : 这行创建了一个向量数组

vector<StudentType> students[classSize] = {};

What you want is this a single vector: 您想要的是一个向量:

vector<StudentType> students;

Where that gets initialized to a zero-length array. 其中将其初始化为零长度数组。

When it comes to adding data you don't need to return from the other method, you can pass in a reference and add to it: 当涉及添加数据时,您不需要从其他方法返回,则可以传入引用并将其添加到其中:

void collectStudentData(vector<StudentType>& students) {
  for (int i = 0; i < classSize; i++) {
    // Read in one at a time
    StudentType  student;
    cout << "Student " << i << "'s name and test score" << endl;
    cin >> student.studentFName >> student.studentLName >> student.testScore;

    // Add to the array
    students.push_back(student);
  }
}

Ideally classSize is either passed in as an argument, or you just type a blank line to end input. 理想情况下, classSize可以作为参数传入,或者您只需键入空行以结束输入。 Using a global variable is really messy and should be strongly discouraged . 使用全局变量确实很麻烦,不建议使用

vector<StudentType> students[classSize]

Is one issue. 是一个问题。 You are not declaring a function that takes a vector, you are declaring a function that takes an array of vectors. 您不是在声明采用向量的函数,而是在声明采用向量数组的函数。

Secondly, if you only applied that change you would be passing an empty vector, you can initialize vector to be a particular size by passing in the size to the constructor. 其次,如果仅应用该更改,则将传递一个空向量,可以通过将大小传递给构造函数来将向量初始化为特定大小。

Furthermore, it seems that you would benefit from passing the students vector by reference 此外,您似乎可以从参考传递学生向量中受益

vector<StudentType>& students

instead, the & creates a reference. 相反,&创建引用。 Right now your code is copying the vector when it is passed into the function 现在,您的代码正在将向量传递给函数时复制该向量

#include <iostream>
#include <vector>
using namespace std;

const int classSize = 1;

struct StudentType {
    string studentFName;
    string studentLName;
    int testScore;
    char grade;
};

void collectStudentData(vector<StudentType>& students) {
    for (int i = 0; i < classSize; i++) {
        cout << "Student " << i << "'s name and test score" << endl;
        cin >> students[i].studentFName >> students[i].studentLName >> students[i].testScore;
    }
    return students;
};

int main() {
    vector<StudentType> students{classSize};
    collectStudentData(students);
    cout << students[0].studentFName << students[0].studentLName << students[0].studentFName;
};

If you wanted to improve the code further, you would use an iterator in the for loop instead, and preferably you wouldn't need to construct the vector in main, and pass it into a function to mutate it. 如果您想进一步改进代码,则可以在for循环中使用迭代器,并且最好不必在main中构造向量,并将其传递给函数进行变异。 You could just construct it and return it from the function. 您可以只构造它并从函数中返回它。

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

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