我想寫一個函數,將通用列表轉換爲矢量,但是,我無法讓我的函數編譯。這裏是我的代碼(它位於.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