【问题标题】:Using a class as the type for STL map使用类作为 STL 映射的类型
【发布时间】: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


【解决方案1】:

我可以看到一些小问题

  1. 默认构造函数应声明为TMyClass()(不带void)。这只是一个样式问题(函数声明的空括号/空括号的区别是针对 C,而不是 C++)。

  2. 为什么 const 的复制构造函数与 TMyClass&amp; 不同?

  3. 为什么会有__fastcall 的噪音?这是我要删除的第一件事,看看它是否有问题。

为了能够将一个元素作为值放入映射中,该类必须是可分配的、可复制构造的并且也是可默认构造的(operator[] 需要能够默认构造一个元素),这一切似乎都还可以案例。

不幸的是,当您进入模板领域时,C++ 编译器会产生几乎无用的错误消息。

【讨论】:

  • 解决办法是去掉“拷贝构造函数2”。
  • @user3455001:接受非常量引用的复制构造函数非常奇怪,但我很惊讶在使用这样的类作为值创建地图时它是一个展示者。事实上,对于其他编译器,这根本不是问题(例如 g++、clang)
猜你喜欢
  • 2010-12-20
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多