C++ 与操作员不匹配!=(操作数类型为指针和对象)

C++ 与操作员不匹配!=(操作数类型为指针和对象),c++,c++11,pointers,operator-overloading,this,C++,C++11,Pointers,Operator Overloading,This,我正在重载==和=运算符,并希望后者引用前者,以避免重复任何代码。这是我写的: bool Date::operator==(常量日期和其他)常量{ bool=年份()=其他.Year(); 对于(int i=0;i取消对指针的引用: return !(*this == other); 您可以尝试像这样重载!=函数: bool Date :: operator != (const Date & other) const { return !(*this == other); }

我正在重载
==
=
运算符,并希望后者引用前者,以避免重复任何代码。这是我写的:

bool Date::operator==(常量日期和其他)常量{
bool=年份()=其他.Year();

对于(int i=0;i取消对指针的引用:

return !(*this == other);

您可以尝试像这样重载
!=
函数:

bool Date :: operator != (const Date & other) const {
    return !(*this == other);
}

您需要取消引用
指针,以便调用它所引用的
日期
对象上的运算符,例如:

bool Date :: operator == (const Date & other) const {
    bool are_equal = ((Year() == other.Year()) && (NumEvents() == other.NumEvents()));

    for (int i = 0; (i < other.NumEvents()) && are_equal; ++i) {
        are_equal = ((*this)[i] == other[i]);
    }

    return are_equal;
}

bool Date :: operator != (const Date & other) const {
    return !((*this) == other);
}
bool Date::operator==(常量日期和其他)常量{
布尔等于((Year()==other.Year())&&(NumEvents()==other.NumEvents());
对于(int i=0;(i
同样
这个[i]==other[i]
几乎肯定不能正确地比较
*这个
其他
是否相等。@Yunnosch-没错。但它肯定是在比较
日期
和(参考)除非类
Date
非常不寻常(例如,具有返回
日期的
运算符[](int)
)然后编译器也会对此进行诊断。OP显然只是在读取编译器的最后一条错误消息-当问题被解决后,可能会回来问一个关于“新”错误消息的类似问题。@Peter我同意(并且确实同意),我可能应该说“真的。而且…”.但是现在我发现了我错过的条件…请参阅问题的注释,以了解有关代码其他问题的提示。