2013-03-04 147 views
1

我試圖超載運算符爲一個作業項目< <。我不斷得到一個錯誤代碼4430缺少類型說明符 - 假設爲int。注意:C++不支持默認輸入。任何幫助將是偉大的!錯誤4430當超載<<運算符

//EmployeeInfo is designed to hold employee ID information 

#ifndef EMPLOYEEINFO_H 
#define EMPLOYEEINFO_H 
#include <iostream> 
#include <ostream> 
using namespace std; 

std::ostream &operator << (std::ostream &, const EmployeeInfo &); 

class EmployeeInfo 
{ 
private: 
    int empID; 
    string empName; 

public: 
    //Constructor 
    EmployeeInfo(); 

    //Member Functions 
    void setName(string); 
    void setID(int); 
    void setEmp(int, string); 
    int getId(); 
    string getName(); 
    string getEmp(int &); 

    //operator overloading 
    bool operator < (const EmployeeInfo &); 
    bool operator > (const EmployeeInfo &); 
    bool operator == (const EmployeeInfo &); 

    friend std::ostream &operator << (std::ostream &, const EmployeeInfo &); 
}; 

friend std::ostream operator<<(std::ostream &strm, const EmployeeInfo &right) 
{ 
    strm << right.empID << "\t" << right.empName; 
    return strm; 
} 
#endif 
+0

請問你的編譯器顯示哪一行的錯誤對應?如果是這樣,哪一行? – 2013-03-04 02:04:32

+0

首先,檢查朋友'std :: ostream運算符<<(std :: ostream&strm,const EmployeeInfo&right)' 定義。在課堂之外不允許「朋友」。 – 2013-03-06 20:13:14

+0

'operator <<'的聲明和定義是不同的。將定義更改爲:'std :: ostream&operator <<(std :: ostream&,const EmployeeInfo&){...}' – 2013-03-06 20:14:12

回答

0

我覺得你的問題是與這條線:

std::ostream &operator << (std::ostream &, const EmployeeInfo &); 

此行EmployeeInfo類的聲明之前出現。換句話說,編譯器還不知道EmployeeInfo是什麼。您也需要到申報轉移到一個點類的聲明,或「預申報」類像這樣經過:

class EmployeeInfo; // "Pre-declare" this class 

std::ostream &operator << (std::ostream &, const EmployeeInfo &); 

class EmployeeInfo 
{ 
    // ... as you have now ... 
}; 
+0

我明白你在說什麼。我改變了它,但仍然收到錯誤。看起來編譯器不會將ostream識別爲數據類型。 – 2013-03-04 11:16:37