我正在尝试创建一个散列表,在一个结构构成的向量中包含一个向量。 v[1].push_back(value);
它给我一个错误:
error C2664: 'void std::vector<node,std::allocator<node>>::push_back(_Ty &&)': cannot convert argument 1 from 'int' to 'const _Ty &'
with
[
_Ty=node
]
note: Reason: cannot convert from 'int' to 'const _Ty'
with
[
_Ty=node
]
note: No constructor could take the source type, or constructor overload resolution was ambiguous
这是我的代码:结构节点{int数据;
node() {
data = 0;
}
};
class hashmap {
public:
vector<vector<struct node>> v;
vector<struct node> n;
hashmap() {
for (int i = 0; i < 5; i++) {
v.push_back(n);
}
}
void insert(int key, int value) {
int index = Hashfunction(key);
v[1].push_back(value);
}
int Hashfunction(int key) {
int index = key % v.size();
return index;
}
};
总是查看完整的错误消息,现代编译器往往会很有帮助。在这种情况下,关键信息是:cannot convert from 'int' to 'const _Ty' with _Ty=node
,如果换出类型模板,则将得到cannot convert from 'int' to 'const node'
。这与嵌套向量无关。使用以下代码,您将看到相同的错误: