的push_back在结构向量不工作
问题描述:
这里是我试图编译代码:的push_back在结构向量不工作
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局部变量进行操作。
代码中没有矢量。 – juanchopanza 2014-09-04 05:19:25
你的意思是使用'std :: vector>'? –
2014-09-04 05:22:48