问题描述
我得到这里提到的错误:
I get the error mentioned here:C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> >
以下是错误(再次):
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(通用)类型的元素与来自二叉搜索树节点的所有值 - 所以我不能使向量const。
The problem is that I have a more complex situation and I don't know how to solve it (without breaking too much code).I have a Binary Search Tree class that is generic. I want to populate a vector of elements of type T(generic) with all the values from the binary search tree nodes - so I can't make the vector const. The function that traverses the tree is recursive.
所以我有:
/*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);
}
/* ------------ */
推荐答案
您正在尝试调用一个带有临时参考的函数。临时只能绑定到const的引用。此外,显示错误实际起源的地方是明智的。
You are trying to call a function that takes a reference with a temporary. A temporary can only bind to a reference to const. Also, it would be wise to show, where the error actually originated.
这篇关于C ++模板错误:没有匹配的函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!