error initializing vector with istream_iterator

J

jmoy.matecon

I get an error while compiling the following program:

int main()
{
vector<int> v(istream_iterator<int>(cin),
istream_iterator<int>());
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
}

The errors I get are [from gcc v. 4.0.3, amd64, ubuntu linux] is:
readint.cc: In function 'int main()':
readint.cc:12: error: request for member 'begin' in 'v', which
is of non-class type 'std::vector<int, std::allocator<int> >
()(std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>
(*)())'
readint.cc:12: error: request for member 'end' in 'v', which is
of non-class type 'std::vector<int, std::allocator<int> >
()(std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>
(*)())'

I understand this to mean that v is getting defined as a function. Is
this as it should be, or is this a problem with my compiler? Using a
temporary variable for the istream_iterator makes the problem go away.

Jyotirmoy Bhattacharya
 
S

Sumit Rajan

I get an error while compiling the following program:

int main()
{
vector<int> v(istream_iterator<int>(cin),
istream_iterator<int>());
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
}

The errors I get are [from gcc v. 4.0.3, amd64, ubuntu linux] is:
readint.cc: In function 'int main()':
readint.cc:12: error: request for member 'begin' in 'v', which
is of non-class type 'std::vector<int, std::allocator<int> >
()(std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>
(*)())'
readint.cc:12: error: request for member 'end' in 'v', which is
of non-class type 'std::vector<int, std::allocator<int> >
()(std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>
(*)())'

I understand this to mean that v is getting defined as a function. Is
this as it should be, or is this a problem with my compiler? Using a
temporary variable for the istream_iterator makes the problem go away.

Please follow this link:
http://groups.google.co.in/group/co...ctor+istream_iterator&rnum=1#3381d9443ce541d8


Regards,
Sumit.
 
S

Sumit Rajan

I understand this to mean that v is getting defined as a function. Is
this as it should be, or is this a problem with my compiler?

No, it is not (please see my earlier post).

Sumit.
 
J

Johan Bengtsson

I get an error while compiling the following program:

int main()
{
vector<int> v(istream_iterator<int>(cin),
istream_iterator<int>());
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));
}

The errors I get are [from gcc v. 4.0.3, amd64, ubuntu linux] is:
readint.cc: In function 'int main()':
readint.cc:12: error: request for member 'begin' in 'v', which
is of non-class type 'std::vector<int, std::allocator<int> >
()(std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>
(*)())'
readint.cc:12: error: request for member 'end' in 'v', which is
of non-class type 'std::vector<int, std::allocator<int> >
()(std::istream_iterator<int, char, std::char_traits<char>, long int>,
std::istream_iterator<int, char, std::char_traits<char>, long int>
(*)())'

I understand this to mean that v is getting defined as a function. Is
this as it should be, or is this a problem with my compiler? Using a
temporary variable for the istream_iterator makes the problem go away.

Unfortunately, this is as it should be. The compiler is very
aggressive in identifying function declarations and will identify the
definition of v as forward declaration of a function accepting two
istream_iterator parameters and returning a vector of integers.
To work around this problem you have to add parentheses around one of
the contructor parameters.

For a more thorough explanation, see item 6 in Effective STL by Scott
Meyers. (ISBN 0-201-74962-9)

/ Johan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top