I have the following C++ code:
typedef istream_iterator<string> isi;
// (1)
vector<string> lineas(isi(cin), isi());
// (2)
//vector<string> lineas;
//copy(isi(cin), isi(), back_inserter(lineas));
typedef vector<string>::iterator vci;
for (vci it = lineas.begin(); it != lineas.end(); ++it)
cout << *it << endl;
However, I get the error while compiling:
test.cpp: In function 'int main(int, char**)':
test.cpp:16: error: request for member 'begin' in 'lineas', which is of non-class 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> > > >(main(int, char**)::isi, main(int, char**)::isi (*)())'
test.cpp:16: error: request for member 'end' in 'lineas', which is of non-class 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> > > >(main(int, char**)::isi, main(int, char**)::isi (*)())'
However, if I replace (1) by (2), it compiles.
I’m using g++ 4.4.0
What’s wrong?
The compiler and you are interpreting this line differently:
For you it is defining and initializing a variable
lineasof typevector<string>with the constructor that takes two iterators.For the compiler you are defining a function
lineasreturning avector<string>and taking two arguments the first of which is anisiand the second of which is a function taking no arguments and returning anisi… With time you will get used to reading compiler errors and what it is reading from your code.The simplest solution is adding an extra pair of parenthesis:
You can find a longer explanation in the C++ FAQ Lite here.