473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

error initializing vector with istream_iterator

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
3 3799

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Aman | last post by:
Hi, could you please tell me why the following program gives a compilation error ? what does this error mean ? note also that when i derefernce the iterator , the contents are displayed. int...
6
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
7
by: SIgnOff | last post by:
What the fastest way to copy strings from file to a vector in STL?
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
7
by: zhouchengly | last post by:
when I use the following code to get integer values from file: void getIntegers() { vector<ItemType items; ifstream ifs("test.dat"); //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£ ItemType item ; while(...
3
by: Eric Lilja | last post by:
Hello, consider the following assignment and my code for it: /* Write a program that does the following: * Integer numbers shall be read from a textfile and stored in a std::vector. The name...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
1
by: efittery | last post by:
I need to modify the following code to handle a vector of pairs of floats. I was just thinking of casting my vector of floats into being a vector of pairs of floats. Alternately, I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.