【发布时间】:2014-03-24 11:40:25
【问题描述】:
我正在使用 Embarcadero C++ Builder XE、Windows 7、32 位。我在使用类作为 STL 映射的类型来编译代码时遇到问题。 在我的简单测试示例中,类声明如下:
#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( );
};
地图中使用的类:
typedef std::map< int, TMyClass> TMyMap;
我正在尝试将一个对象插入到地图中:
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”。不过,我不知道这是否是问题的原因。
标签: c++ map stl c++builder