I want to load all the lines from a text file into a vector
cout
:
<
You have fallen victim to the Most Vexing Parse, where the compiler sees your declaration as a function strings
returning a vector
, taking two arguments:
istream_iterator
called file
istream_iterator
.To eliminate the vexing parse, use an extra pair of parentheses around the first argument:
vector strings((istream_iterator(file)) , istream_iterator());
// ^ ^
or, alternatively in C++11, use curly braces for the strings
constructor
vector strings{istream_iterator(file) , istream_iterator()};
// ^ ^
NOTE: Clang warns you about it through -Wvexing-parse
(on by default).