重载时错误4430<<操作者

时间:2013-03-04 01:50:51

标签: visual-c++

我正在尝试重载<<家庭作业项目的操作员。我不断收到错误代码4430缺少类型说明符 - 假设为int。注意:C ++不支持default-in。任何帮助都会很棒!

//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

1 个答案:

答案 0 :(得分: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 ...
};