QSet不允许我添加自己的类型

时间:2011-04-08 17:33:28

标签: qt hash set

我正在使用QT,我有一个QSet,我想添加vtkSmartPointer<vtkImageData>。我的语法是QSet<vtkSmartPointer<vtkImageData> >。但是,我收到以下编译错误。

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

尝试匹配参数列表'(const vtkSmartPointer)' 与

[
    T=vtkImageData
]

对我来说,这看起来我必须编写自己的散列函数

  1. 这是对的吗?
  2. 如果是这样,我该怎么做?
  3. 如果没有发生什么事?
  4. 我是来自Java的QT的新手,我从来不必担心编写自己的哈希函数。

    由于

1 个答案:

答案 0 :(得分:2)

你是正确的,你必须为vtkSmartPointer类编写自己的散列函数,因为Qt没有提供。但是Qt确实为指针(qHash(const T *))提供了一个哈希函数,所以你可以试试这样的东西:

template<typename T>
uint qHash(const vtkSmartPointer<T> &p)
{
    return qHash(p.GetPointer());
}
相关问题