【问题标题】:Class vector has no member类向量没有成员
【发布时间】:2019-08-22 22:56:13
【问题描述】:

我正在收集姓名和考试成绩来填充向量。函数和 main 方法都无法识别结构的成员。我怎样才能看到它的成员?或者有没有更好的方法来使用函数使用用户输入填充结构向量?

我搜索了其他类似的帖子,但似乎只是我错过了一个简单的代码错误。

#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': 不是'std::vector>'的成员

【问题讨论】:

  • vector&lt;StudentType&gt; students[classSize] = {}; 更改为vector&lt;StudentType&gt; students;(并再次在collectStudentData 的声明中)。
  • @PaulSanders 还需要进行其他几项更改
  • 你最好使用向量的向量而不是向量的数组
  • @NutCracker 我认为他们只想要一个向量但对语言语法感到困惑
  • @MM 是的,OP 在读入向量之前至少需要在向量中分配空间。

标签: c++ vector member


【解决方案1】:

这一行创建了一个向量数组

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

你想要的是一个单一的向量:

vector<StudentType> students;

它被初始化为一个零长度数组。

在添加不需要从其他方法返回的数据时,可以传入引用并添加到它:

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

理想情况下,classSize 要么作为参数传入,要么您只需键入一个空行来结束输入。使用全局变量真的很麻烦,应该强烈反对

【讨论】:

  • 谢谢!希望这能解决我的问题
【解决方案2】:
vector<StudentType> students[classSize]

是一个问题。您不是在声明一个接受向量的函数,而是在声明一个接受向量数组的函数。

其次,如果您只应用该更改,您将传递一个空向量,您可以通过将大小传递给构造函数来将向量初始化为特定大小。

此外,您似乎会从通过引用传递学生向量中受益

vector<StudentType>& students

相反,& 创建了一个引用。现在您的代码在将向量传递给函数时正在复制它

#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;
};

如果你想进一步改进代码,你可以在 for 循环中使用迭代器,最好你不需要在 main 中构造向量,并将它传递给一个函数来改变它。你可以构造它并从函数中返回它。

【讨论】:

  • 通过引用接受向量并按值返回它没有多大意义。可能该函数不应该带参数,只返回一个向量
  • 另外,你在最后一行读的越界(向量索引从 0 开始)
  • 感谢您的帮助。不敢相信我以为它是从 1 开始的。我以前做过这个,哈哈
猜你喜欢
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多