student类中,没有输出操作符 << 的重载定义
时间: 2023-08-18 15:03:49 浏览: 37
如果在Student类中没有定义输出操作符<<的重载,那么你将无法直接使用cout语句来输出Student对象的信息。你需要手动编写一个输出函数来显示Student对象的信息。以下是一个示例:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int age;
string id;
public:
// 构造函数
Student(string n, int a, string i) {
name = n;
age = a;
id = i;
}
// 输出函数
void display() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "ID: " << id << endl;
}
};
int main() {
Student s("Tom", 18, "1001");
s.display(); // 调用输出函数显示学生信息
return 0;
}
```
在上面的示例中,我们手动编写了一个名为display的输出函数来显示Student对象的信息。在main函数中,我们创建了一个Student对象s,并使用display函数来输出该对象的信息。