2012-06-15 90 views
-4

沒有匹配的功能我習慣這裏提到的錯誤: C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> >C++模板錯誤:呼叫

這裏是(再次)錯誤:

main.cpp: In function ‘int main()’: 
main.cpp:21:21: error: no matching function for call to ‘Test<int>::foo(std::vector<int, std::allocator<int> >)’ 
main.cpp:21:21: note: candidate is: 
main.cpp:14:6: note: void Test<T>::foo(std::vector<T>&) [with T = int] 
main.cpp:14:6: note: no known conversion for argument 1 from ‘std::vector<int, std::allocator<int> >’ to ‘std::vector<int, std::allocator<int> >&’ 

的問題是,我有一個更復雜的情況而且我不知道如何解決它(不會破壞太多的代碼)。 我有一個通用的二進制搜索樹類。我想用類型T(泛型)的元素向量填充來自二叉搜索樹節點的所有值 - 所以我不能使向量爲常量。遍歷樹的函數是遞歸的。

所以我必須:

/*main*/ 

BST<int> t; 

t.add(21); 
t.add(12); 
//.... etc. 

vector<int> elements; 

t.inorder(elements); 
/* ------------ */ 

和:

/*BST.h*/ 
template<typename T> 
class BST{ 
    //.... 
    Node<T>* root; 
    //.... 
    void inorder_rec(Node<T>* root, vector<T>& result); 
    void inorder(vector<T>& result); 
    //.... 
}; 

template<typename T> 
void BST<T>::inorder_rec(Node<T>* root, vector<T>& result){ 
    // recursive call for the child nodes 
} 

void BST<T>::inorder(vector<T>& result){ 
    inorder_rec(this->root, result); 
} 
/* ------------ */ 
+0

你應該發佈你得到的實際錯誤。另外,我認爲你在'BST'類聲明中缺少'class'。 – juanchopanza

+0

'Test :: foo(std :: vector &)'? – hmjd

+0

和您的成員函數需要返回類型。 – juanchopanza

回答

3

您試圖調用一個函數,它有一個臨時的參考。臨時只能綁定到對const的引用。另外,顯示錯誤實際發生的地方是明智的。

0

這是你的實際代碼? inorder和inorder_rec的定義需要返回類型。否則,這部分代碼看起來很好,沒有臨時看到:

vector<int> elements; 
t.inorder(elements); 

愚蠢的問題,但你保存你的文件?或者這個錯誤來自代碼的不同部分?

+0

我編輯了代碼(我寫了它匆忙 - 這不是實際的代碼)。 –

+0

@ user1458849錯誤消息是指'Test :: foo(vector &)',而不是'BST :: inorder(vector &)'。什麼是您的實際錯誤信息? –