我创建了一个 for 循环来打印输入的学生信息,但是当我使用 cout << st.printInfo() << endl;
打印它们时, 我得到了 no operator "<<" matches these operants
学生类(class):
class Student {
private:
string nameSurname;
int score;
public:
void printInfo();
void setName(string _nameSurname) { nameSurname = _nameSurname; }
void setScore(int _score) { score = _score; }
Student() {
nameSurname = "Not Entered";
score = 0;
}
~Student() {}
};
打印函数:
void Student::printInfo()
{
cout << "-----------------" << endl;
cout << "Name and Surname : " << nameSurname << endl;
cout << "Score : " << score << endl;
cout << "-----------------" << endl;
}
主要功能:
vector<Student> v;
string nameSurname;
int score;
Student st;
for (int i = 0; i < v.size() + 1; i++)
{
cout << "Enter " << i + 1 << " Student Name and Surname : " << endl;
cin.ignore();
getline(cin, nameSurname);
st.setName(nameSurname);
cout << "Enter Student's Score : " << endl;
cin >> score;
st.setScore(score);
v.push_back(st);
}
错误在下面的 for 循环部分中显示出来。该 for 循环也在 main 函数中。
for (int i = 0; i < v.size(); i++)
{
cout << st.printInfo() << endl;
}
最佳答案
printInfo
函数返回 void
和 std::ostream
没有打印设备void
.
更改您的 printInfo
返回一个值。
或调用您的 printInfo
单独运行。
或者传递 std::ostream
给你的printInfo
功能。
最好的方法是重载 operator<<
在你的Student
类(class)。
编辑 1: 细节:
你的for
循环应该是:
for (int i = 0; i < v.size(); i++)
{
v[i].printInfo();
cout << "\n";
}
关于c++ - 将 Vector 与类一起使用时没有运算符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33638615/