1

我正在尝试重载 << 运算符,以便像这样打印出学生对象:

Student: <name>,<number>,<email address>,<year>,<major>

尝试编译显示以下内容的程序时,我不断收到错误消息:

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

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

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

我这个类的头文件:

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

};

如果有人可以帮助我弄清楚是什么给了我这个问题,我将不胜感激。

4

1 回答 1

6

你需要

#include <string>

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

于 2013-02-04T03:04:04.197 回答