使用QSet遇到的编译错误

时间:2021-01-20 15:01:39
 

今天下午写了如下一段代码:

QSet pointSet; pointSet.insert(QPoint());
编译后却得到一个编译错误:

error C2665: 'qHash' : none of the 16 overloads could convert all the argument types
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(62): could be 'uint qHash(char)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(63): or 'uint qHash(uchar)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(64): or 'uint qHash(signed char)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(65): or 'uint qHash(ushort)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(66): or 'uint qHash(short)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(67): or 'uint qHash(uint)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(68): or 'uint qHash(int)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(69): or 'uint qHash(ulong)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(77): or 'uint qHash(long)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(78): or 'uint qHash(quint64)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(86): or 'uint qHash(qint64)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(87): or 'uint qHash(QChar)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(88): or 'uint qHash(const QByteArray &)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(89): or 'uint qHash(const QString &)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(90): or 'uint qHash(const QStringRef &)'
1>        d:/qt/4.6.2-msvc2005/include/qtcore/../../src/corelib/tools/qhash.h(91): or 'uint qHash(const QBitArray &)'

当时百思不得其解。于是只好去查看Qt的官方文档,看了官方文档的一段话之后,豁然开朗,问题也就迎刃而解。我们来看下这段让我豁然开朗的话:

QSet's value data type must be an assignable data type. You cannot, for example, store a QWidget as a value; instead, store a QWidget *. In addition, the type must provide operator==() , and there must also be a global qHash() function that returns a hash value for an argument of the key's type . See the QHash documentation for a list of types supported by qHash().

关键的一句是:必须要有一个全局的qHash函数,该函数用于给键类型的参数产生一个哈希值。

而Qt默认只为以下类型提供qHash函数:

  • uint qHash ( const QString & key )
  • uint qHash ( const QXmlNodeModelIndex & index )
  • uint qHash ( char key )
  • uint qHash ( uchar key )
  • uint qHash ( signed char key )
  • uint qHash ( ushort key )
  • uint qHash ( short key )
  • uint qHash ( uint key )
  • uint qHash ( int key )
  • uint qHash ( ulong key )
  • uint qHash ( long key )
  • uint qHash ( quint64 key )
  • uint qHash ( qint64 key )
  • uint qHash ( QChar key )
  • uint qHash ( const QByteArray & key )
  • uint qHash ( const QBitArray & key )
  • uint qHash ( const T * key )
  • uint qHash ( const QPair & key )

现在明白了,程序产生编译错误的原因是我没有为QPoint类型提供一个全局的qHash函数。于是我提供了如下的qHash函数,程序成功编译通过。

      uint qHash(const QPoint &point)  
  1. {  
  2.     QString valueStr(QString::number(point.x()) + QString::number(point.y()));  
  3.     return valueStr.toUInt();