本文介绍了C++ 模板错误:没有用于调用 std::vector<int, std::allocator<int> 的匹配函数>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法弄清楚为什么我收到以下代码的错误:
I can't manage to figure out why I get an error for the following code:
template <typename T>
class Test{
void foo(vector<T>& v);
};
template <typename T>
void Test<T>::foo(vector<T>& v){
//DO STUFF
}
int main(){
Test<int> t;
t.foo(vector<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> >&’
我做错了什么?
推荐答案
您不能将临时对象绑定到非 const
引用.
You can't bind a temporary to a non-const
reference.
要么将您的签名更改为:
Either change your signature to:
void foo(vector<T> const& v);
或者不传递一个临时的:
or don't pass a temporary:
vector<int> temp;
t.foo(temp);
这篇关于C++ 模板错误:没有用于调用 std::vector<int, std::allocator<int> 的匹配函数>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!