在C++错误中重载<<运算符错误::: binary'<<':找不到运算符的运算符,它采用类型为'const std :: string的右手操作数

cad*_*d4j 1 c++ oop operator-overloading

我试图重载<<运算符,以便打印出一个学生对象:

Student: <name>,<number>,<email address>,<year>,<major>
Run Code Online (Sandbox Code Playgroud)

我在尝试编译程序时遇到错误:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
Run Code Online (Sandbox Code Playgroud)

我的实现文件中的函数如下所示:

ostream& operator<<(ostream& output, const Student& student)
{
  output << "Student: " << student.name <<", " << student.m_Number <<", " << student.email <<", " << student.year << ", " << student.major << endl;
  return output;
}
Run Code Online (Sandbox Code Playgroud)

我的这个类的头文件:

#include <iostream>

using namespace std;

class Student
{
public:
   //Default constructor
   Student();
   //Set the student information
   Student setStudent(string[], int);
   //Retrieve the Student M_Number
   int getM_Number();

   friend ostream& operator << (ostream& output, const Student& student);



private:
   //Student's name, M_Number, Email Address, Year in School and Major
   string name;
   int m_Number;
   string email;
   string year;
   string major;

};
Run Code Online (Sandbox Code Playgroud)

如果有人能帮我弄清楚是什么给了我这个问题,我真的很感激.

ric*_*ici 6

你需要

#include <string>
Run Code Online (Sandbox Code Playgroud)

为了获得适当的声明 operator<<(std::ostream&, std::string)