QSet 追加自定义对象
QSet append custom Object
我有以下class
class Test{
int a;
int b;
};
我想要的只是有一个 QSet<Test>
我将在其中插入一些测试对象。
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QSet<Test> setTest;
setTest.insert(Test());
return QCoreApplication::exec();
}
问题是我出于某种原因收到以下错误,我不知道为什么或如何解决它。
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(118): error
C2665: 'qHash': none of the 24 overloads could convert all the
argument types C:\Qt.14.2\msvc2017_64\include\QtCore/qlocale.h(62):
note: could be 'uint qHash(const QLocale &,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qregexp.h(56): note: or
'uint qHash(const QRegExp &,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(108): note:
or 'uint qHash(std::nullptr_t,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(105): note:
or 'uint qHash(QLatin1String,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(104): note:
or 'uint qHash(const QBitArray &,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(103): note:
or 'uint qHash(QStringView,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(101): note:
or 'uint qHash(const QStringRef &,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(100): note:
or 'uint qHash(const QString &,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(98): note: or
'uint qHash(const QByteArray &,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(97): note: or
'uint qHash(const QChar,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(95): note: or
'uint qHash(long double,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(93): note: or
'uint qHash(double,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(92): note: or
'uint qHash(float,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(91): note: or
'uint qHash(qint64,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(87): note: or
'uint qHash(quint64,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(86): note: or
'uint qHash(long,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(80): note: or
'uint qHash(ulong,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(79): note: or
'uint qHash(int,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(78): note: or
'uint qHash(uint,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(77): note: or
'uint qHash(short,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(76): note: or
'uint qHash(ushort,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(75): note: or
'uint qHash(signed char,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(74): note: or
'uint qHash(uchar,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(73): note: or
'uint qHash(char,uint) noexcept'
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(118): note:
while trying to match the argument list '(const T)'
with
[
T=Test
] C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(117): note:
see reference to function template instantiation 'uint
qHash(const T &,uint) noexcept()' being compiled
with
[
Key=Test,
T=Test
] C:\Qt.14.2\msvc2017_64\include\QtCore/qhash.h(946): note: while compiling class template member function 'QHashNode<Key,T>
**QHash<Key,T>::findNode(const Key &,uint *) const'
with
[
Key=Test,
T=QHashDummyValue
] C:\Qt.14.2\msvc2017_64\include\QtCore/qhash.h(790): note: see reference to function template instantiation 'QHashNode<Key,T>
**QHash<Key,T>::findNode(const Key &,uint *) const' being compiled
with
[
Key=Test,
T=QHashDummyValue
] C:\Qt.14.2\msvc2017_64\include\QtCore/qhash.h(786): note: while compiling class template member function
'QHash<T,QHashDummyValue>::iterator
QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)'
with
[
T=Test,
Key=Test
] C:\Qt.14.2\msvc2017_64\include\QtCore/qset.h(208): note: see reference to function template instantiation
'QHash<T,QHashDummyValue>::iterator
QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)'
being compiled
with
[
T=Test,
Key=Test
] C:\Qt.14.2\msvc2017_64\include\QtCore/qset.h(257): note: see reference to class template instantiation
'QHash<T,QHashDummyValue>' being compiled
with
[
T=Test
] C:\Users\vlad.dumitru\CLionProjects\Test\main.cpp(11): note: see reference to class template instantiation 'QSet' being
compiled
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(118): error
C2056: illegal expression NMAKE : fatal error U1077:
'C:\PROGRA~2\MICROS~217\BUILDT~1\VC\Tools\MSVC16~1.270\bin\Hostx64\x64\cl.exe'
: return code '0x2' Stop. NMAKE : fatal error U1077: '"C:\Program
Files (x86)\Microsoft Visual
Studio17\BuildTools\VC\Tools\MSVC.16.27023\bin\HostX64\x64\nmake.exe"'
: return code '0x2' Stop. NMAKE : fatal error U1077: '"C:\Program
Files (x86)\Microsoft Visual
Studio17\BuildTools\VC\Tools\MSVC.16.27023\bin\HostX64\x64\nmake.exe"'
: return code '0x2' Stop. NMAKE : fatal error U1077: '"C:\Program
Files (x86)\Microsoft Visual
Studio17\BuildTools\VC\Tools\MSVC.16.27023\bin\HostX64\x64\nmake.exe"'
: return code '0x2' Stop.
你们知道我该如何解决这个问题吗?
更新:
class Test {
int a;
int b;
bool operator==(const Test &e1) const {
return e1.a == a && e1.b == b;
}
uint qHash(const Test &key, uint seed) {
return qHash(key.a, seed) ^ key.b;
}
};
我确实编写了我需要的那 2 个函数,但我现在遇到了一个新错误:
error C2664: 'uint Test::qHash(const Test &,uint)': cannot convert
argument 1 from 'const int' to 'const Test &'
我不能调用 qHash(key.a, seed)
因为“对类型 'const Test' 的引用无法绑定到类型 'const int' 的左值”
QSet
实现为 QHash
,因此您必须在 class 中实现 operator==()
并在相同的命名空间中实现 qHash()
函数你的 class.
它会使全局 qHash()
过载并且可以具有这些签名之一
uint qHash(K key);
uint qHash(const K &key);
uint qHash(K key, uint seed);
uint qHash(const K &key, uint seed);
否则 QSet
将无法对您的数据进行哈希处理。
这里可以看官方例子
https://doc.qt.io/qt-5/qhash.html#qhash
这是关于 QSet
https://doc.qt.io/qt-5/qset.html#details
的文档页面
为了完整起见,我在这里链接它,但不清楚这个要求。
我有以下class
class Test{
int a;
int b;
};
我想要的只是有一个 QSet<Test>
我将在其中插入一些测试对象。
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
QSet<Test> setTest;
setTest.insert(Test());
return QCoreApplication::exec();
}
问题是我出于某种原因收到以下错误,我不知道为什么或如何解决它。
C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(118): error C2665: 'qHash': none of the 24 overloads could convert all the argument types C:\Qt.14.2\msvc2017_64\include\QtCore/qlocale.h(62): note: could be 'uint qHash(const QLocale &,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qregexp.h(56): note: or
'uint qHash(const QRegExp &,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(108): note: or 'uint qHash(std::nullptr_t,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(105): note: or 'uint qHash(QLatin1String,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(104): note: or 'uint qHash(const QBitArray &,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(103): note: or 'uint qHash(QStringView,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(101): note: or 'uint qHash(const QStringRef &,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(100): note: or 'uint qHash(const QString &,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(98): note: or 'uint qHash(const QByteArray &,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(97): note: or 'uint qHash(const QChar,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(95): note: or 'uint qHash(long double,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(93): note: or 'uint qHash(double,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(92): note: or 'uint qHash(float,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(91): note: or 'uint qHash(qint64,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(87): note: or 'uint qHash(quint64,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(86): note: or 'uint qHash(long,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(80): note: or 'uint qHash(ulong,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(79): note: or 'uint qHash(int,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(78): note: or 'uint qHash(uint,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(77): note: or 'uint qHash(short,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(76): note: or 'uint qHash(ushort,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(75): note: or 'uint qHash(signed char,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(74): note: or 'uint qHash(uchar,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(73): note: or 'uint qHash(char,uint) noexcept' C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(118): note: while trying to match the argument list '(const T)' with [ T=Test ] C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(117): note: see reference to function template instantiation 'uint qHash(const T &,uint) noexcept()' being compiled with [ Key=Test, T=Test ] C:\Qt.14.2\msvc2017_64\include\QtCore/qhash.h(946): note: while compiling class template member function 'QHashNode<Key,T> **QHash<Key,T>::findNode(const Key &,uint *) const' with [ Key=Test, T=QHashDummyValue ] C:\Qt.14.2\msvc2017_64\include\QtCore/qhash.h(790): note: see reference to function template instantiation 'QHashNode<Key,T> **QHash<Key,T>::findNode(const Key &,uint *) const' being compiled with [ Key=Test, T=QHashDummyValue ] C:\Qt.14.2\msvc2017_64\include\QtCore/qhash.h(786): note: while compiling class template member function 'QHash<T,QHashDummyValue>::iterator QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)' with [ T=Test, Key=Test ] C:\Qt.14.2\msvc2017_64\include\QtCore/qset.h(208): note: see reference to function template instantiation 'QHash<T,QHashDummyValue>::iterator QHash<T,QHashDummyValue>::insert(const Key &,const QHashDummyValue &)' being compiled with [ T=Test, Key=Test ] C:\Qt.14.2\msvc2017_64\include\QtCore/qset.h(257): note: see reference to class template instantiation 'QHash<T,QHashDummyValue>' being compiled with [ T=Test ] C:\Users\vlad.dumitru\CLionProjects\Test\main.cpp(11): note: see reference to class template instantiation 'QSet' being compiled C:\Qt.14.2\msvc2017_64\include\QtCore/qhashfunctions.h(118): error C2056: illegal expression NMAKE : fatal error U1077: 'C:\PROGRA~2\MICROS~217\BUILDT~1\VC\Tools\MSVC16~1.270\bin\Hostx64\x64\cl.exe' : return code '0x2' Stop. NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\VC\Tools\MSVC.16.27023\bin\HostX64\x64\nmake.exe"' : return code '0x2' Stop. NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\VC\Tools\MSVC.16.27023\bin\HostX64\x64\nmake.exe"' : return code '0x2' Stop. NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\VC\Tools\MSVC.16.27023\bin\HostX64\x64\nmake.exe"' : return code '0x2' Stop.
你们知道我该如何解决这个问题吗?
更新:
class Test {
int a;
int b;
bool operator==(const Test &e1) const {
return e1.a == a && e1.b == b;
}
uint qHash(const Test &key, uint seed) {
return qHash(key.a, seed) ^ key.b;
}
};
我确实编写了我需要的那 2 个函数,但我现在遇到了一个新错误:
error C2664: 'uint Test::qHash(const Test &,uint)': cannot convert argument 1 from 'const int' to 'const Test &'
我不能调用 qHash(key.a, seed)
因为“对类型 'const Test' 的引用无法绑定到类型 'const int' 的左值”
QSet
实现为 QHash
,因此您必须在 class 中实现 operator==()
并在相同的命名空间中实现 qHash()
函数你的 class.
它会使全局 qHash()
过载并且可以具有这些签名之一
uint qHash(K key);
uint qHash(const K &key);
uint qHash(K key, uint seed);
uint qHash(const K &key, uint seed);
否则 QSet
将无法对您的数据进行哈希处理。
这里可以看官方例子
https://doc.qt.io/qt-5/qhash.html#qhash
这是关于 QSet
https://doc.qt.io/qt-5/qset.html#details
的文档页面
为了完整起见,我在这里链接它,但不清楚这个要求。