插入指向向量的指针时出错
我有以下 CPP 代码片段和关联的错误消息:
代码片段
struct node{
char charVal;
bool childNode;
struct node *leftChild;
struct node *rightChild;
};
vector<std::pair<int,struct node*> > nodeCountList;
struct node *nodePtr = new struct node;
nodeCountList.push_back(1,nodePtr);
错误消息
error: no matching function for call to ‘std::vector<std::pair<int, node*>, std::allocator<std::pair<int, node*> > >::push_back(int&, node*&)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::pair<int, node*>, _Alloc = std::allocator<std::pair<int, node*> >]
请帮助我解决错误消息问题。
干杯
I have the following CPP code snippet and the associated error message:
Code snippet
struct node{
char charVal;
bool childNode;
struct node *leftChild;
struct node *rightChild;
};
vector<std::pair<int,struct node*> > nodeCountList;
struct node *nodePtr = new struct node;
nodeCountList.push_back(1,nodePtr);
Error message
error: no matching function for call to ‘std::vector<std::pair<int, node*>, std::allocator<std::pair<int, node*> > >::push_back(int&, node*&)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:602: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::pair<int, node*>, _Alloc = std::allocator<std::pair<int, node*> >]
Please help me troubleshoot the error message.
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要推送 std::pair。
You need to push a std::pair.
您试图将两个参数传递给
nodeCountList.push_back
,而它只接受一个参数。相反,首先创建一个std::pair
,其中包含您想要的两个项目。然后,使用该std::pair
作为参数调用nodeCountList.push_back
。You are trying to pass two arguments to
nodeCountList.push_back
, which only accepts one argument. Instead, first create astd::pair
with the two items you want in it. Then, callnodeCountList.push_back
with thatstd::pair
as the argument.您是否尝试过先将“节点”转换为类型,然后使用您的模板?也许这样效果会更好。
Have you tried turining "node" into a type first, and then using your template? Maybe this will work better.