我想写一个函数,将通用列表转换为矢量,但是,我无法让我的函数编译。这里是我的代码(它位于.h文件中):C++无法返回矢量模板
template <class T>
inline std::vector<T> list2vector(std::list<T> &l)
{
std::vector<T> v;
v.insert(v.begin(),l.begin(),l.end());
return v;
}
任何人都可以指出我在这里错过了什么吗? 编译器错误是:
find_rpeat.cpp:85: error: invalid initialization of non-const reference of type
?std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&?
from a temporary of type
?std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >?
也许你需要'常量的std ::函数签名列表'? –
arrowd
2012-04-02 07:26:29
这段代码为我编译[http://ideone.com/p3CEt](http://ideone.com/p3CEt) – ks1322 2012-04-02 07:28:11
哇,加了const做了个窍门,你能解释一下为什么这样可行吗? – user788171 2012-04-02 07:28:30