没有匹配的功能我习惯这里提到的错误: 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);
}
/* ------------ */
你应该发布你得到的实际错误。另外,我认为你在'BST'类声明中缺少'class'。 – juanchopanza
'Test :: foo(std :: vector &)'? –
hmjd
和您的成员函数需要返回类型。 – juanchopanza