我很努力地解决这个问题,不管它的简单性,但我不知道确切的错误在哪里。在程序中传递一个函数有一个struct C++
这个问题是对结构的一种实践,它需要用户输入2名学生的姓名和年龄,并返回使用struct的老年人姓名和返回学生姓名的函数。
#include <iostream>
#include <string>
using namespace std;
struct student{
string name;
int age;
};
student getOlder(student s1, student s2);
int main()
{
student s1, s2, Max;
cout << "Enter the first sudent's name" << endl;
getline(cin, s1.name);
cout << "Enter the first sudent's age" << endl;
cin >> s1.age;
cout << "Enter the second sudent's name" << endl;
getline(cin, s2.name);
cout << "Enter the second sudent's age" << endl;
cin >> s2.age;
Max = getOlder(s1, s2);
cout << Max << " is the older student " << endl;
}
student getOlder(student s1, student s2)
{
if (s1.age > s2.age){
cout << s1.name << endl;
}
cout << s2.name << endl;
return result;
}
我真的很感谢,但我有一个小问题,编译器现在突出显示在行“”Max“之前的操作数”“Max <<”是更老的学生“<< endl; “因为没有操作符匹配这些操作数,这意味着什么以及如何纠正这个错误? – Nashat12
你没有为你的结构提供'operator <<'重载,所以'cout'不知道如何输出它无论是重载'operator <<'还是单独输出成员 –