English 中文(简体)
C++ 职能清单
原标题:C++ List Unique() Function

The following errors are always been pointing to my List.

我的班子档案

Point3D operator==(const Point3D &p1) const;

在我的档案中

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

In my main file

//declaration
list<Point3D> l_3D;

l_3D.unique(); <-- the error point to this 

错误信息

..... In member function  void std::list<_Tp,_Alloc>::unique() [with_Tp = Point3D,_Alloc = std:: allocator<Point3D>]:

Instantiated from here

error: could not convert  Point3D::operator==(const Point3D&) const (((const Point3D&)((const Point3D*)__next. std::_List_iterator<_Tp>::operator*[with_Tp = Point3D]())))  to  bool 

To the kind souls out there, I thank you in advance.

最佳回答

在您的声明中,请将<条码>第3D改为<条码>。

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;
}

甚至更好(根据@同上的评论):

bool Point3D::operator==(const Point3D &p2) const
{
    return (getX() == p2.getX() && getY() == p2.getY() && getZ() == p2.getZ());
}

Generally the signature of your definition should match the signature of your declaration.

问题回答

变化:

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

To this:

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;
}

In your class file (I assume you mean a header file), you declare operator== as returning Point3D when it should be returning bool. Also, your operator== has a different number of arguments in your class (header?) file and your cpp file.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签