我正在使用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”。不过,我不知道这是否是问题的原因。 –