C++ C++;List Unique()函数

C++ C++;List Unique()函数,c++,list,unique,operator-keyword,C++,List,Unique,Operator Keyword,以下错误总是指向我的列表 在我的班级档案中 Point3D operator==(const Point3D &p1) const; 在我的.cpp文件中 bool operator==(Point3D &p1, Point3D &p2) { if ( p1.getX() == p2.getX() && p1.getY() == p2.getY() && p1.getZ() == p2.getZ() ) retur

以下错误总是指向我的列表

在我的班级档案中

Point3D operator==(const Point3D &p1) const;
在我的.cpp文件中

bool operator==(Point3D &p1, Point3D &p2)
{
    if ( p1.getX() == p2.getX() && p1.getY() == p2.getY() && p1.getZ() == p2.getZ() )
        return true;
    else
        return false;
}
在我的主文件中

//declaration
list<Point3D> l_3D;

l_3D.unique(); <-- the error point to this 
//声明
列表l_3D;

l_3D.unique() 在声明中返回
Point3D
,它应该是
bool

Point3D operator==(const Point3D &p1) const;
^^^^^^^
should be bool
上面看起来像是将操作符声明为成员函数,然后将其实现为自由函数。你必须决定它是哪一个。如果使用成员函数,请将实现更改为:

bool Point3D::operator==(const Point3D &p2) const
{
    if ( getX() == p2.getX() && getY() == p2.getY() && getZ() == p2.getZ() )
        return true;
    else
        return false;
}
或者更好(根据@ibids评论):


通常,定义的签名应与声明的签名相匹配。

在声明中返回
Point3D
,该值应为
bool

Point3D operator==(const Point3D &p1) const;
^^^^^^^
should be bool
上面看起来像是将操作符声明为成员函数,然后将其实现为自由函数。你必须决定它是哪一个。如果使用成员函数,请将实现更改为:

bool Point3D::operator==(const Point3D &p2) const
{
    if ( getX() == p2.getX() && getY() == p2.getY() && getZ() == p2.getZ() )
        return true;
    else
        return false;
}
或者更好(根据@ibids评论):

通常,定义的签名应与声明的签名相匹配。

更改:

bool operator==(Point3D &p1, Point3D &p2)
{
    if ( p1.getX() == p2.getX() && p1.getY() == p2.getY() && p1.getZ() == p2.getZ() )
        return true;
    else
        return false;
}
为此:

bool operator==(const Point3D &p1, const Point3D &p2)
{
    if ( p1.getX() == p2.getX() && p1.getY() == p2.getY() && p1.getZ() == p2.getZ() )
        return true;
    else
        return false;
}
更改:

bool operator==(Point3D &p1, Point3D &p2)
{
    if ( p1.getX() == p2.getX() && p1.getY() == p2.getY() && p1.getZ() == p2.getZ() )
        return true;
    else
        return false;
}
为此:

bool operator==(const Point3D &p1, const Point3D &p2)
{
    if ( p1.getX() == p2.getX() && p1.getY() == p2.getY() && p1.getZ() == p2.getZ() )
        return true;
    else
        return false;
}

在类文件(我假设您指的是头文件)中,当它应该返回
bool
时,您将
operator==
声明为返回
Point3D
。另外,您的
操作符==
在类(头?)文件和cpp文件中有不同数量的参数。

在类文件(我假设您指的是头文件)中,您将
操作符===
声明为在应该返回
bool
时返回
Point3D
。此外,您的
运算符==
在类(头?)文件和cpp文件中具有不同数量的参数。

此外,如果(某些布尔表达式)返回true,则不使用
if;否则返回false
,让
返回一些布尔表达式更为惯用if;否则返回false
,让
返回一些布尔表达式更为惯用也是一种糟糕的风格;否则返回false。此外,如果(…)返回true,则编写
是不好的风格;否则返回false。使用此方法时,必须在标头中的类声明之外写入声明(如
class Point3D{};extern bool operator==(const Point3D&,const Point3D&);
)。使用此方法时,必须在标头中的类声明之外写入声明(如
class Point3D{});外部布尔运算符==(常量点3D&,常量点3D&);
)。