2012-06-15 126 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 &)'。什么是您的实际错误信息? –