2009-08-29 33 views
1

我有以下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*> >] 

請幫我排查錯誤信息。

歡呼

回答

7

你需要推一個std :: pair。

nodeCountList.push_back(std::make_pair(1,nodePtr)); 
1

您是否嘗試過turining「節點」爲類型,然後再使用您的模板?也許這會更好。

2

您試圖將兩個參數傳遞給nodeCountList.push_back,它只接受一個參數。相反,首先創建一個std::pair與您想要在其中的兩個項目。然後,請撥打nodeCountList.push_back作爲參數std::pair

相關問題