【发布时间】:2012-11-08 16:10:24
【问题描述】:
我在 C++ 中有一个 STL 映射,其中键是一个无符号整数,值是一个类,其构造函数是:
Foo::Foo(unsigned int integerValue){
//Some stuff
}
在其他课程中,我在标题处声明了 std::map:
private:
std::map<unsigned int, Foo> *m_mapFoo;
在 cpp 文件中,我创建了它并插入了 Foo 的实例:
m_mapFoo = new std::map<unsigned int, Foo>;
m_mapFoo->insert(0, new Foo(0));
m_mapFoo->insert(1, new Foo(1));
但我在插入方法中遇到以下错误:
no matching function for call to ‘std::map<unsigned int, Foo, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, Foo> > >::insert(const unsigned int&, Foo*)’
find 方法的类似问题:
m_mapFoo.find(0)->second->someFunctionIntoFooClass();
错误的地方如下:
request for member ‘find’ in ‘((Foo*)this)->Foo::m_mapGeoDataProcess’, which is of non-class type ‘std::map<unsigned int, Foo, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, Foo> > >*’
附加说明:我没有 Foo 复制构造函数,但我认为这不是问题。
对理解这个错误有帮助吗?
【问题讨论】:
-
如果您没有复制构造函数,您希望如何复制到地图中?
-
我已经阅读了stackoverflow,有些人说你必须有,有些人说你不需要。但我不知道这是否仅适用于 [] 插入而不适用于插入方法。见stackoverflow.com/questions/1478330/…
-
您需要
insert的复制构造函数。不过emplace不需要它。 -
@Pubby OP 可能有编译器合成的复制构造函数。
标签: c++ dictionary stl std