C++ 向类构造函数传递初始值设定项\u对列表时发生实例化错误
我似乎找不到类的构造函数有什么问题。当我尝试实例化一个表类对象时,我得到一个C++ 向类构造函数传递初始值设定项\u对列表时发生实例化错误,c++,map,constructor,initializer-list,C++,Map,Constructor,Initializer List,我似乎找不到类的构造函数有什么问题。当我尝试实例化一个表类对象时,我得到一个 error: no matching function for call to ‘std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >::map(std::initializer_list<std::pair<int, int> >&)
error: no matching function for call to ‘std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >::map(std::initializer_list<std::pair<int, int> >&)’
错误:调用“std::map::map(std::initializer\u list&)”时没有匹配的函数
下面是我当前的表类实现
template <class T1,class T2>
class table {
public:
table() = default;
table(std::initializer_list<std::pair<T1,T2>> args) :slots(args) {}
table(const table& other) :slots(other.slots) {}
table& operator= (table other) {
swap(*this,other);
return *this;
}
~table() = default;
typedef typename std::map<T1,T2>::iterator iterator;
T2& operator[](T1 key) {
return slots[key];
}
unsigned erase(T1 key) {
return slots.erase(key);
}
unsigned erase(iterator start, iterator finish) {
return slots.erase(start,finish);
}
size_t size() {
return slots.size();
}
iterator begin() {
return slots.begin();
}
iterator end() {
return slots.end();
}
bool key_exists(T1 key) {
return (slots.find(key) != slots.end());
}
bool value_exists(T2 value) {
return (std::find_if(slots.begin(),slots.end(),[value](std::pair<T1,T2> a) {
return (a.value == value);
}) != slots.end());
}
std::map<T1,T2> slots;
private:
void swap(table& a,table& b) {
using std::swap;
swap(a.slots,b.slots);
}
};
模板
类表{
公众:
table()=默认值;
表(std::初始值设定项_list args):插槽(args){
表(const表和其他):插槽(其他.slots){}
表和运算符=(表其他){
掉期(*本,其他);
归还*这个;
}
~table()=默认值;
typedef typename std::map::迭代器迭代器;
T2和操作员[](T1键){
返回槽[键];
}
无符号擦除(T1键){
返回插槽。擦除(键);
}
无符号擦除(迭代器开始、迭代器完成){
返回插槽。擦除(开始、完成);
}
大小{
返回槽。大小();
}
迭代器begin(){
返回slots.begin();
}
迭代器结束(){
返回槽。结束();
}
布尔键_存在(T1键){
return(slots.find(key)!=slots.end());
}
布尔值_存在(T2值){
返回(std::find_if(slots.begin()、slots.end()、[value](std::pair a){
返回值(a.value==值);
})!=slots.end());
}
std::映射槽;
私人:
无效掉期(表a、表b){
使用std::swap;
交换(a插槽,b插槽);
}
};
可能很简单,这对中的第一个类型可能是常量?我认为@JoachimPileborg的评论可能就是解释。但请务必发布完整的代码。最小,但完整,包括实例化。是的,它是常量。谢谢你的帮助!