2014-09-04 44 views
-2

這裏是我試圖編譯代碼:的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>*’ 

我無法理解問題在哪裏。

+3

代碼中沒有矢量。 – juanchopanza 2014-09-04 05:19:25

+1

你的意思是使用'std :: vector >'? – 2014-09-04 05:22:48

回答

2

您的結構定義更改爲以下:

struct node{ 
    vector< pair<int, float> > neighbors; 
}; 

這將允許您對添加到載體的鄰居。請注意,對將被值複製到向量中,這是我假設您正在嘗試使用wvertex局部變量進行操作。