2014-03-24 42 views
2

我正在使用Embarcadero C++ Builder XE,Windows 7,32位。我有問題使用類作爲STL地圖的類型來編譯代碼。 在我的簡單測試,例如類聲明如下:使用類作爲STL映射的類型

typedef std::map< int, TMyClass> TMyMap; 

我試圖對象插入到地圖:

#include <map> 
class TMyClass 
{ 
private: // User declarations 
int cVal1; 
int cVal2; 

public: 
    TMyClass& __fastcall operator = (TMyClass& aMyClassObj); 

public:  // User declarations 
    __fastcall TMyClass(void); 
    __fastcall TMyClass(int aVal1, int aVal2); 

    __fastcall TMyClass(const TMyClass& aMyClassObj); // copy constructor 1 
    __fastcall TMyClass(  TMyClass& aMyClassObj);  // copy constructor 2 
    __fastcall ~TMyClass(); 
}; 

該類在地圖上使用

TMyMap sMyMap; 
TMyClass  sMyClassObj(10, 10); 
aMyMap[ 1] = sMyClassObj; 

最後一行給出的編譯錯誤:

[BCC32 Error] xtree(29): E2285 Could not find a match for 'pair<const int,TMyClass>::pair(const pair<const int,TMyClass>)' 
    Full parser context 
    xtree(28): decision to instantiate: _Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node::_Node(_Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node *,_Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node *,_Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node *,const pair<const int,TMyClass> &,char) 
    --- Resetting parser context for instantiation... 
    U_TestKompilacji.cpp(10): #include U_TestKompilacji.h 
    U_TestKompilacji.h(5): #include C:\Programms\Embarcadero\RAD Studio\8.0\include\boost_1_39\boost\tr1\tr1\map 
    map(20): #include C:\Programms\Embarcadero\RAD Studio\8.0\Quickrep505C\../include/dinkumware/map 
    map(5): #include c:\Programms\embarcadero\rad studio\8.0\include\dinkumware\xtree 
    xtree(8): namespace std 
    xtree(13): class _Tree_nod<_Traits> 
    xtree(25): class _Tree_nod<_Traits>::_Node 
    xtree(28): parsing: _Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node::_Node(_Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node *,_Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node *,_Tree_nod<_Tmap_traits<int,TMyClass,less<int>,allocator<pair<const int,TMyClass> >,0> >::_Node *,const pair<const int,TMyClass> &,char) 

我一直在試圖找到一個解決方案很多天。在我以前使用過的Borland C++ Builder 6.0中沒有這樣的問題。 這個類是否有一些要求用作地圖中的值?

+3

賦值運算符應該帶一個'const'引用,你幾乎可以肯定擺脫那個奇怪的「複製構造函數2」。不過,我不知道這是否是問題的原因。 –

回答

0

我可以看到幾個小問題

  1. 默認構造函數應該聲明TMyClass()(無void)。這只是一個樣式問題(函數聲明的空括號/ void區分是針對C而不是C++的)。

  2. 爲什麼複製構造函數不同於const或不是TMyClass&

  3. 爲什麼所有那__fastcall噪音?這是我要刪除的第一件事情,看看它是否有問題。

爲了能夠把一個元素在地圖中的價值類必須是可分配的,拷貝構造也缺省構造(operator[]需要能夠以缺省方式構造元素)和所有似乎確定在這種情況下。

不幸的是,C++編譯器因輸入模板領域時產生幾乎無用的錯誤消息而聞名。

+0

解決方案是刪除「複製構造函數2」。 – user3455001

+0

@ user3455001:接受非const引用的複製構造函數非常奇怪,但我很驚訝這是使用值這樣的類創建地圖時的炫目者。事實上,對於其他編譯器來說,這根本不是問題(例如g ++,clang) – 6502