1

我正在尝试为家庭作业项目重载 << 运算符。我不断收到错误代码 4430 缺少类型说明符 - 假定为 int。注意:C++ 不支持默认输入。任何帮助都会很棒!

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

1 回答 1

0

我认为您的问题在于这一行:

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

这一行出现在EmployeeInfo类的声明之前。换句话说,编译器还不知道 anEmployeeInfo是什么。您要么需要将该声明移至类声明之后的某个点,要么像这样“预先声明”该类:

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

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

class EmployeeInfo
{
    // ... as you have now ...
};
于 2013-03-04T02:08:50.453 回答