2013-02-04 82 views
1

我試圖按順序超載< <運算符打印出的學生對象,例如:在C++中重載<<運算符錯誤:::二進制'<<':找不到運算符類型爲'const std :: string'的操作符

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; 

}; 

如果有人可以幫助我弄清楚發生了什麼給我這個問題,我真的很感激它。

回答

6

您需要

#include <string> 

,以獲得operator<<(std::ostream&, std::string)

相應的聲明