使用类作为STL映射的类型
Using a class as the type for STL map
我使用的是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:ProgrammsEmbarcaderoRAD Studio8.0includeboost_1_39boosttr1tr1map
map(20): #include C:ProgrammsEmbarcaderoRAD Studio8.0Quickrep505C../include/dinkumware/map
map(5): #include c:Programmsembarcaderorad studio8.0includedinkumwarextree
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中没有这样的问题,我以前使用过它。类作为映射中的值是否有某种要求? 我可以看到一些小问题
-
默认构造函数应该声明为
TMyClass()
(没有void
)。这只是一个风格问题(函数声明的空括号/void区别是针对C的,而不是c++的)。 -
为什么
const
和TMyClass&
的复制构造函数不同? -
为什么有那么多
__fastcall
的噪音?这是我要删除的第一件事,看看它是否有问题。
为了能够将一个元素作为映射中的值,类必须是可赋值的、可复制构造的和默认构造的(operator[]
需要能够默认构造一个元素),在这种情况下,这一切似乎都是可以的。
不幸的是,当您进入模板领域时,c++编译器以产生几乎无用的错误消息而闻名。
相关文章:
- 值到类型的运行时映射
- 拥有映射的现代方法,该映射可以指向或引用已在堆栈上分配的不同类型的数据
- 如何将高维数据映射到特征类型?
- 如何使映射键具有两种不同的数据类型?
- 值和类型的简洁双向静态 1:1 映射
- 当 map 是一个整数数组并且由 operator[] 创建时,它是否初始化其映射类型
- 编译按映射类型获取元素的映射错误元组
- 模板.参数包扩展 - 重映射类型
- C++函数来返回基于参数的比较器通用映射类型?
- ATL ActiveX DLL 作为映射类型图像和数据加载了两次
- 如何在 C++ 中获取映射类型指针,映射
- 映射类型为的队列容器
- 如何使用boost::mutex作为std::map中的映射类型
- 正向声明映射的映射类型和C++11
- std::map何时调用映射类型的构造函数
- 基于映射类型c++解析和转换csv
- 模板类的映射类型签名(声明)
- c++映射类型的属性
- 使用映射类型会产生GCC错误:预期的未限定id在' for '之前
- 在 EIGEN 中单独声明和初始化映射类型矩阵