顺便说一下,我在Arch Linux上使用了eclipse和g ++(我不到一个星期前运行pacman -Syu,所以一切都是最新的)。

每当我尝试编译时,Eclipse都会产生一个错误:

#ifndef DATE_HPP_
#define DATE_HPP_

using namespace std;

class Date {
public:
    int Year;
    char Month;
    char Day;
    char HH;
    char MM;
    char ss;
    Date();

    /*
     * Overloaded Operator Functions
     */
    //Assignments
    Date operator=(Date input);
    //Comparisons
    bool operator==(Date& rhs);
    bool operator!=(Date& rhs);
    bool operator<(Date& rhs);
    bool operator>(Date& rhs);
    bool operator<=(Date& rhs);
    bool operator>=(Date& rhs);
    //Conversion
    operator char*();
    operator std::string();
    ostream& operator<<(ostream& os, const Date& date); //TROUBLE LINE
};

#endif /* DATE_HPP_ */


Eclipse在operator <
ostream& operator<<(const Date& date);


它抱怨它必须有两个。我究竟做错了什么?

最佳答案

运算符的两个参数重载必须是非成员函数。可以将其移出类定义,也可以添加friend使其成为非成员Friend函数,以更合理的方式进行。

单参数重载没有用,因为它在对象实例为左操作数时使用。

07-24 09:45
查看更多