开发者

call of overloaded 'qHash(const double&)' is ambiguous

I get this error when I try to convert a QList to a QSet.

QList<double> x_pts;
x_pts << 4.1;
x_pts &开发者_运维知识库lt;< 2.2;
x_pts << 2.2;
x_pts << 1.3;
qSort(x_pts);
QSet<double> list = x_pts.toSet();

Any idea why i am getting this error? It works fine if I change the type to int or double*. But double should work as well.

All i want to do is to remove duplicates from my list. I want to find a way to do it without iterating the list myself.

Any help in removing this error or another function to remove duplicates will be appreciated.

Thanks,

Rush


The compiler error is due to the fact that Qt simply hasn't defined a qHash() function that takes in a double. The reason for this is that it is generally a bad idea to use floating point numbers as keys in a hash (in this case your QSet uses a hash internally).

You say you are trying to remove duplicates, but the concept of equality among floating point numbers is fuzzy in C++, as it turns out to be quite difficult to say that any two floating point numbers are truly equal.

(See the C++ faq regarding this subject)

The solution to your problem is going to be either:

a) Don't use floating point numbers

b) Define your own qHash function that satisfies the requirements of your program in a way that is satisfactory. Defining a qHash( double ) in your implentation file would be sufficient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜