2011-04-08 50 views
2

我正在使用QT並且我有一個QSet我希望將vtkSmartPointer<vtkImageData>添加到。我的語法是QSet<vtkSmartPointer<vtkImageData> >。但是,我收到以下編譯錯誤。QSet不讓我添加我自己的類型

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 &)' 

試圖參數列表「(常量vtkSmartPointer)」 與

[ 
    T=vtkImageData 
] 

匹配對我來說,這看起來像我有我自己寫的哈希函數

  1. 這是正確?
  2. 如果是這樣,我該怎麼做?
  3. 如果沒有發生什麼事情?

我是來自Java的QT的新手,我從來不必擔心編寫自己的散列函數。

感謝

回答

2

你是,你必須寫自己的哈希函數爲vtkSmartPointer類,因爲Qt不提供一個正確的。然而Qt爲指針提供散列函數(qHash(const T *)),所以你可以嘗試這樣的事情:

template<typename T> 
uint qHash(const vtkSmartPointer<T> &p) 
{ 
    return qHash(p.GetPointer()); 
} 
+0

這會去哪裏?這將保證指向不同數據的兩個指針會返回不同的散列函數嗎? – Jon 2011-04-08 18:18:28

+0

當你聲明你的'QSet >'時,你需要有這個定義。所以如果你需要從多個源文件中使用它,你可以把它放在同一個源文件中,並在頭文件中或頭文件中使用'QSet'。這個函數只是使用Qt的qHash()函數,它根據指針的值計算散列值。 Qt是一個成熟且經過良好測試的庫,所以我確信他們的哈希函數可以工作。 – 2011-04-08 18:24:34

+0

謝謝,編譯得很好。 – Jon 2011-04-08 18:30:41