结构中矢量的push_back不起作用
push_back for vector in struct not working
以下是我试图编译的代码:
int main(){
struct node{
pair<int, float>* neighbors;
};
pair<int, float> wvertex;
int VCount, v1, v2;
float w;
cin >> VCount;
node* graph_nodes[VCount+1];
while( cin >> v1 ){
cin >> v2 >> w;
wvertex.first = v2;
wvertex.second = w;
graph_nodes[v1]->neighbors.push_back(wvertex);
}
return 0;
}
但是,它在编译时给出了一个错误:
In function ‘int main()’:
error: request for member ‘push_back’ in ‘graph_nodes[v1]->main()::node::neighbors’, which is of non-class type ‘std::pair<int, float>*’
我不明白问题出在哪里。
将结构定义更改为以下内容:
struct node{
vector< pair<int, float> > neighbors;
};
这将允许您向向量邻居添加对。注意,对将按值复制到向量中,我假设您无论如何都在尝试使用wvertex局部变量。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用