2


I tried to compile this code snippet but I got compiler error :( ! Compile with Visual Studio 2010

#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <iostream>

using namespace std;

int main() {
    string s( "Well well on" );
    istringstream in( s );
    vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() );
    copy( v.begin(), v.end(), ostream_iterator<string>( cout, "\n" ) );
}

Errors:

Error   1   error C2228: left of '.begin' must have class/struct/union  c:\visual studio 2008 projects\vector test\vector test\main.cpp 13  vector test
Error   2   error C2228: left of '.end' must have class/struct/union    c:\visual studio 2008 projects\vector test\vector test\main.cpp 13  vector test

What happened? vector was constructed correctly, how could I not be able to call it?

Best regards,

roxrook
  • 13,511
  • 40
  • 107
  • 156
  • You should check out the "The Definitive C++ Book Guide and List" at http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list, "Effective STL" especially – Eugen Constantin Dinca Dec 22 '10 at 17:28

3 Answers3

6

I think this

vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() );

is parsed as a function declaration:

vector<string> v( istream_iterator<string> in, istream_iterator<string> );

This is usually called "C++' most-vexing parse".

I think a few extra parentheses will cure this:

vector<string> v( (istream_iterator<string>(in)), (istream_iterator<string>()) );
Community
  • 1
  • 1
sbi
  • 219,715
  • 46
  • 258
  • 445
1

This is an example of the so-called most vexing parse. It;'s a gotcha that stings many C++ programmers.

Basically, this code doesn't mean what you think it means:

vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() );

Instead of declaring a variable of type vector<string>, you are actually declaring a function named v that returns vector<string>.

To fix this, use operator= like this:

vector<string> v = vector<string>( istream_iterator<string>( in ), istream_iterator<string>() );
Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

The parser thinks the following line is declaring a function:

vector<string> v( istream_iterator<string>( in ), istream_iterator<string>() );

Change your main to this and it will compile:

int main() 
{
    string s( "Well well on" );
    istringstream in( s );
    istream_iterator<string> start = istream_iterator<string>(in);
    vector<string> v(start, istream_iterator<string>());
    copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));
}
Zac Howland
  • 15,777
  • 1
  • 26
  • 42