By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 2,015 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

error initializing vector with istream_iterator

P: n/a
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

Jun 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

jm**********@gmail.com wrote:
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/com...81d9443ce541d8
Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>

Jun 30 '06 #2

P: n/a

jm**********@gmail.com wrote:
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.
--
Sumit Rajan <su*********@gmail.com>

Jun 30 '06 #3

P: n/a
jm**********@gmail.com writes:
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
Jun 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.