I get the error mentioned here:
C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> >
Here is the error(again):
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> >&’
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.
So I have:
/*main*/
BST<int> t;
t.add(21);
t.add(12);
//.... etc.
vector<int> elements;
t.inorder(elements);
/* ------------ */
and:
/*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);
}
/* ------------ */
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.