【发布时间】:2016-10-10 17:06:53
【问题描述】:
我有一张像 map<string, unique_ptr<Base>> variables 这样的地图,我正在尝试将数据插入地图 variables.insert(make_pair("foo", new Int(10))),但我遇到了以下错误:
error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, std::unique_ptr<Base>>::insert(std::pair<const char*, Int*>)’
variables.insert(make_pair("test", new Int(10)));
error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
template<typename _Pair, typename = typename
这是我的代码:
class Base {
public:
Base() {};
virtual ~Base() {};
};
class Int : public Base {
public:
Int(int i) {
this->i = i;
}
Int operator=(int i) {
this->i = i;
}
int i;
};
void set() {
map<string, unique_ptr<Base>> variables;
variables.insert(make_pair("test", new Int(10)));
}
我想我需要一双新的眼睛来看看这个我不确定这是什么问题,谢谢!
编辑
我正在尝试制作异构映射,并且每种数据类型都有一个类。但是不管有多少,我仍然会遇到同样的错误。
【问题讨论】:
-
请发minimal reproducible example。函数体之外的语句是无效的语法。
-
static_cast<>也可能会有所帮助。 -
我没有从您的代码中得到这些错误:coliru.stacked-crooked.com/a/893f119bceb7514d
-
@aschepler 出于某种原因删除
unique_ptr<>使其工作
标签: c++ class c++11 dictionary