cadavid4j
cadavid4j

Reputation: 171

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.

Upvotes: 1

Views: 1138

Answers (1)

rici
rici

Reputation: 241671

You need to

#include <string>

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

Upvotes: 6

Related Questions