开发者

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜