使用QSet遇到的编译错误
2011-03-09 18:00
363 查看
今天下午写了如下一段代码:
error C2665: 'qHash' : none of the 16 overloads could convert all the argument types
1> include/qtcore/../../src/corelib/tools/qhash.h(62): could be 'uint qHash(char)'
1> include/qtcore/../../src/corelib/tools/qhash.h(63): or 'uint qHash(uchar)'
1> include/qtcore/../../src/corelib/tools/qhash.h(64): or 'uint qHash(signed char)'
1> include/qtcore/../../src/corelib/tools/qhash.h(65): or 'uint qHash(ushort)'
1> include/qtcore/../../src/corelib/tools/qhash.h(66): or 'uint qHash(short)'
1> include/qtcore/../../src/corelib/tools/qhash.h(67): or 'uint qHash(uint)'
1> include/qtcore/../../src/corelib/tools/qhash.h(68): or 'uint qHash(int)'
1> include/qtcore/../../src/corelib/tools/qhash.h(69): or 'uint qHash(ulong)'
1> include/qtcore/../../src/corelib/tools/qhash.h(77): or 'uint qHash(long)'
1> include/qtcore/../../src/corelib/tools/qhash.h(78): or 'uint qHash(quint64)'
1> include/qtcore/../../src/corelib/tools/qhash.h(86): or 'uint qHash(qint64)'
1> include/qtcore/../../src/corelib/tools/qhash.h(87): or 'uint qHash(QChar)'
1> include/qtcore/../../src/corelib/tools/qhash.h(88): or 'uint qHash(const QByteArray &)'
1> include/qtcore/../../src/corelib/tools/qhash.h(89): or 'uint qHash(const QString &)'
1> include/qtcore/../../src/corelib/tools/qhash.h(90): or 'uint qHash(const QStringRef &)'
1> 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函数,程序成功编译通过。
QSet pointSet; pointSet.insert(QPoint());
编译后却得到一个编译错误:
error C2665: 'qHash' : none of the 16 overloads could convert all the argument types
1> include/qtcore/../../src/corelib/tools/qhash.h(62): could be 'uint qHash(char)'
1> include/qtcore/../../src/corelib/tools/qhash.h(63): or 'uint qHash(uchar)'
1> include/qtcore/../../src/corelib/tools/qhash.h(64): or 'uint qHash(signed char)'
1> include/qtcore/../../src/corelib/tools/qhash.h(65): or 'uint qHash(ushort)'
1> include/qtcore/../../src/corelib/tools/qhash.h(66): or 'uint qHash(short)'
1> include/qtcore/../../src/corelib/tools/qhash.h(67): or 'uint qHash(uint)'
1> include/qtcore/../../src/corelib/tools/qhash.h(68): or 'uint qHash(int)'
1> include/qtcore/../../src/corelib/tools/qhash.h(69): or 'uint qHash(ulong)'
1> include/qtcore/../../src/corelib/tools/qhash.h(77): or 'uint qHash(long)'
1> include/qtcore/../../src/corelib/tools/qhash.h(78): or 'uint qHash(quint64)'
1> include/qtcore/../../src/corelib/tools/qhash.h(86): or 'uint qHash(qint64)'
1> include/qtcore/../../src/corelib/tools/qhash.h(87): or 'uint qHash(QChar)'
1> include/qtcore/../../src/corelib/tools/qhash.h(88): or 'uint qHash(const QByteArray &)'
1> include/qtcore/../../src/corelib/tools/qhash.h(89): or 'uint qHash(const QString &)'
1> include/qtcore/../../src/corelib/tools/qhash.h(90): or 'uint qHash(const QStringRef &)'
1> 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) { QString valueStr(QString::number(point.x()) + QString::number(point.y())); return valueStr.toUInt(); }
相关文章推荐
- 使用QSet遇到的编译错误
- 使用Qt Creator编译cpp遇到错误cc1plus.exe: out of memory allocating
- 使用studio编译中遇到的两个的错误_'D:\jdk1.7.0_80\bin\java.exe'' finished with non-zero exit value 1 等
- 使用NDK(r7)编译时用到boost 遇到的错误。
- 使用studio编译中遇到的错误ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/
- 编译使用CEF2623遇到的错误解决办法
- 使用map迭代器中遇到的问题——非const转成const类型造成的编译错误
- 使用 VS 2012 编译 GTest 遇到的编译错误
- 使用repo下载google源码,跟编译 ,遇到的错误解决
- 使用 VS 2012 编译 GTest 遇到的编译错误
- 使用xcodebuild命令行编译iOS应用遇到的一个错误及解决办法
- 使用studio编译中遇到的两个的错误_'D:\jdk1.7.0_80\bin\java.exe'' finished with non-zero exit value 1 等
- 使用studio编译中遇到的两个的错误_'D:\jdk1.7.0_80\bin\java.exe'' finished with non-zero exit value 1 等
- 使用Qt Creator编译cpp遇到错误cc1plus.exe: out of memory allocating
- 在VC中使用switch语句时遇到“error C2360: initialization of 'k' is skipped by 'case' label”的编译错误。
- 使用NDK 编译C/C++程序遇到 crtbegin_so.o: No such file or directory 等错误
- 使用 VS 2012 编译 GTest 遇到的编译错误
- 在VC中使用switch语句时遇到“error C2360: initialization of 'k' is skipped by 'case' label”的编译错误。
- Oprofile 编译使用过程中遇到的几个错误
- 使用Openjdk 编译Android4.4时遇到"error: unmappable character for encoding ASCII"错误的解决方法