我正在使用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中沒有這樣的問題。 這個類是否有一些要求用作地圖中的值?
賦值運算符應該帶一個'const'引用,你幾乎可以肯定擺脫那個奇怪的「複製構造函數2」。不過,我不知道這是否是問題的原因。 –