无法返回CMap

时间:2013-08-06 14:01:42

标签: c++ visual-c++ mfc

使用Visual Studio 2012,以下编译:

void Stuff()
{
  CMap <int, int, int, int > myMap;
}

但是当我尝试返回这样的CMap时,就像这样:

CMap<int, int, int, int> GetEncodingMap()
{
  CMap <int, int, int, int> encodingMap;
  return encodingMap;
}

我收到以下编译错误:

1>          c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afx.h(559) : see declaration of 'CObject::CObject'
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\atlmfc\include\afx.h(534) : see declaration of 'CObject'
1>          This diagnostic occurred in the compiler generated function 'CMap<KEY,ARG_KEY,VALUE,ARG_VALUE>::CMap(const CMap<KEY,ARG_KEY,VALUE,ARG_VALUE> &)'
1>          with
1>          [
1>              KEY=int,
1>              ARG_KEY=int,
1>              VALUE=int,
1>              ARG_VALUE=int
1>          ]

无论我在CMap中使用什么类型,似乎总是如此。

有人可以帮我理解他的情况吗?是否有可能从函数返回CMap?

3 个答案:

答案 0 :(得分:2)

CMap是一个不可复制的对象,因为它继承自CObject,它有一个私有的复制构造函数。如果你想让它可以复制,你将需要从CMap继承并提供你自己的,虽然我不确定它是多么可行。

答案 1 :(得分:0)

这只是MFC试图阻止你复制CMap对象的一种错综复杂的方式。您无法复制CMap对象。

答案 2 :(得分:0)

GetEncodingMap()方法不会创建对象,只是声明并返回。在返回之前尝试创建。