简体   繁体   中英

Cannot access vector when constructing with istream_iterator range


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,

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>()) );

This is an example of the so-called most vexing parse . It;'sa 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>() );

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"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM