2013-03-04 100 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