473,394 Members | 2,048 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,394 software developers and data experts.

Odd behaviour with istream_iterator

I'm using gcc 3.3.5. This code:

std::set<std::stringt(std::istream_iterator<std::s tring>(std::cin),
std::istream_iterator<std::string>());

gives a strange error message:

error: cannot use `::' in parameter declaration

If I try it this way:

std::istream_iterator<std::stringis(std::cin);
std::istream_iterator<std::stringeof();
std::set<std::stringt(is, eof);

it also gives an error:

error: no matching function for call to `std::set<*snip*>::set(*snip*)'

Strangely enough, this:

std::set<std::stringt((std::istream_iterator<std:: string>(std::cin)),
std::istream_iterator<std::string>());

compiles and works just fine. The only difference is the extra
parentheses around the first parameter. I can't get the second version
above to compile regardless of any amount of parentheses anywhere.

Even more strangely, if I add the extra parentheses around the
second parameter, like this:

std::set<std::stringt((std::istream_iterator<std:: string>(std::cin)),
(std::istream_iterator<std::string>()));

it once again doesn't compile:

error: syntax error before `)' token

I don't understand this. What's going on?
May 17 '07 #1
2 1785
edd
On 17 May, 17:57, Juha Nieminen <nos...@thanks.invalidwrote:
I'm using gcc 3.3.5. This code:

std::set<std::stringt(std::istream_iterator<std::s tring>(std::cin),
std::istream_iterator<std::string>());

gives a strange error message:

error: cannot use `::' in parameter declaration

If I try it this way:

std::istream_iterator<std::stringis(std::cin);
std::istream_iterator<std::stringeof();
std::set<std::stringt(is, eof);

it also gives an error:

error: no matching function for call to `std::set<*snip*>::set(*snip*)'
I believe it's a bug in gcc 3.3. It thinks you're declaring a function
called 't'. Similarly for your second error, it thinks a function
called 'is' is being declared.
http://www.gnu.org/software/gcc/bugs.html#known (Parse errors for
"simple" code)

gcc 3.4 and onwards should cope fine.

Here is another possible work around (don't have 3.3 on this machine
to test the theory):

typedef std::istream_iterator<std::stringiter_t;
iter_t is = iter_t(std::cin);
iter_t eof;
std::set<std::stringt(is, eof);

Kind regards,

Edd

May 17 '07 #2
On May 17, 6:57 pm, Juha Nieminen <nos...@thanks.invalidwrote:
I'm using gcc 3.3.5. This code:

std::set<std::stringt(std::istream_iterator<std::s tring>(std::cin),
std::istream_iterator<std::string>());

gives a strange error message:

error: cannot use `::' in parameter declaration
Compiler tries to look on your line as on a function declaration
(function t, which receives istream_iterator with improper param name
std::cin (cant use qualified names in function param declaration), and
a pointer to function which receives nothing, returns
istream_iterator(unnamed second parameter of t). Finally, function t
returns a set).
If I try it this way:

std::istream_iterator<std::stringis(std::cin);
std::istream_iterator<std::stringeof();
std::set<std::stringt(is, eof);

here you have another function delcaration - eof (receives nothing,
returns istream_iterator), thats why you cant toss it to t's
constructor second param.

Remember: function declarations can be local, and they're given
priority when deciding what the line of code means.
>
std::set<std::stringt((std::istream_iterator<std:: string>(std::cin)),
std::istream_iterator<std::string>());
because you cant use parentheses around a param in a function
declaration, this is correctly interpreted as a set constructor call.

Dont know about the last lines you posted. They compile fine on VS
2005 compiler.

May 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Rudolph | last post by:
The member function basic_ios::operator!() returns the bool result of the basic_ios::fail() function which is true if either failbit or badbit is set (This is per p. 616 of TC++PL by B. Stroustrup...
3
by: NPC | last post by:
Hi, Is there any way to use an istream_iterator<> in a way which inserts each element at the end of a newline character rather than a space character? Could be it looks for any type of whitespace...
9
by: Alex Vinokur | last post by:
------ foo.cpp : BEGIN ------ #include <cassert> #include <vector> #include <string> #include <iostream> #include <iterator> #include <fstream> using namespace std;
2
by: ben | last post by:
Is istream_iterator<> and ostream_iterator<> supposed NOT to be included in <iostream>? The following example compiles in VC 7.1 but not with gcc 3.4.3 #include <iostream> #include <deque>...
0
by: Richo11 | last post by:
This is probably a silly question but I am learning sorry... I am trying to read in a series of strings from standard input using the istream_iterator member function, how does...
2
by: alberto | last post by:
I am learning STL with the book STL Tutorial and Reference guide (1 edition), the following example don't run : int main() { // Initialize array a with 10 integers: int a = {12, 3, 25, 7, 11,...
2
by: Marcus Kwok | last post by:
I am writing a program to read in a file, do some processing, then write it out to a different file. I really like the idiom I use for output: // std::vector<std::string> vec; // std::ofstream...
3
by: jmoy.matecon | last post by:
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"));...
5
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.