Template-related error messages can be confusing at times. The problem is that the standard library does not define an overload of operator <<
for inserting std::vector
(or any other container, for that matter) into a std::ostream
. So the compiler fails to find a suitable overload for operator <<
, and reports this failure as best as it's able (which is unfortunately not too good/readable in your case).
If you want to stream an entire container, you can use std::ostream_iterator
for that:
auto v = std::vector<int>{1, 2, 3};
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, " "));
As for why you're getting precisely this cryptic error, it helps to analyse the full error message:
prog.cpp: In function ‘int main()’:
prog.cpp:13:37: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
std::cout << std::vector<int>{1,2,3};
^
In file included from /usr/include/c++/4.8/iostream:39:0,
from prog.cpp:3:
/usr/include/c++/4.8/ostream:602:5: error: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::vector<int>]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
^
There is apparently a template overload of operator<<
which takes a lhs argument of type std::ostream&&
and a rhs argument of the templated type; it exists to allow insertion into temporary streams. Since it's a template, it becomes the best match for the expression in your code. However, std::cout
is an lvalue, so it cannot bind to std::ostream&&
. Hence the error.