简体   繁体   中英

Overloading the << operator in C++ error ::: binary '<<' : no operator found which takes a right-hand operand of type 'const std::string

I'm attempting to overload the << operator in order to print out a student object as such:

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

I keep getting an error when trying to compile the program that says:

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

The function in my implementation file looks like this:

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

My header file for this class:

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

};

If anyone could help me figure out what's giving me this issue, I would really appreciate it.

You need to

#include <string>

in order to get the appropriate declaration of operator<<(std::ostream&, std::string)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM