472,126 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

istringstream class question

Why does this:

string AWord(string& line)
{
return line;
}

bool MYOBJECT::MyFunction(string &line)
{
int day;

istringstream(AWord(line)) >> day;
}

produce this:

cd e:\rr\thirdedition\import\
make -f import.mak TARGET=pc
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/import.o -Wno-deprecated import.cpp
myfile.cpp: In member function `bool MYOBJECT::MyFunction(std::string&)':
myfile.cpp:845: parse error before `>>' token
make: *** [pc/myfile.o] Error 1

Compilation exited abnormally with code 2 at Sat Jan 01 23:43:37
???
--
% Randy Yates % "Midnight, on the water...
%% Fuquay-Varina, NC % I saw... the ocean's daughter."
%%% 919-577-9882 % 'Can't Get It Out Of My Head'
%%%% <ya***@ieee.org> % *El Dorado*, Electric Light Orchestra
http://home.earthlink.net/~yatescr
Jul 22 '05 #1
8 1954
Randy Yates wrote:
Why does this:

string AWord(string& line)
{
return line;
}

bool MYOBJECT::MyFunction(string &line)
{
int day;

istringstream(AWord(line)) >> day;
You should return something here.
}


Other than the missing return statement, the code looks fine, assuming you have
the necessary includes and using declarations/directives.

It would be helpful to post a self-contained example that compiles when the
offending line is commented out. E.g., no references to undefined classes like
MYOBJECT.

Jonathan
Jul 22 '05 #2
On Sun, 02 Jan 2005 04:47:06 GMT, Randy Yates <ya***@ieee.org> wrote:
Why does this:

string AWord(string& line) "line" should be declared as a const & if you aren't changing it...
{
return line;
}

bool MYOBJECT::MyFunction(string &line)
{
int day;

istringstream(AWord(line)) >> day; Here you need to return something...
}

produce this:

cd e:\rr\thirdedition\import\
make -f import.mak TARGET=pc
g++ -c -DTARGET_PC -Wall -g -I /. -I /mingw/include/ -I /devstudio/vc/include -o ./pc/import.o -Wno-deprecated import.cpp
myfile.cpp: In member function `bool MYOBJECT::MyFunction(std::string&)':
myfile.cpp:845: parse error before `>>' token
make: *** [pc/myfile.o] Error 1

Compilation exited abnormally with code 2 at Sat Jan 01 23:43:37


One of two things, or possibly both:

(1) You forgot to include <sstream>
(2) You need to make the namespace std visible, either by writing
std::istringstream or put "using std::istringstream;" inside your
function body.

Since gcc apparently finds std::string, judging from the error
message, I will guess that (1) is the case?

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3
On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates
<ya***@ieee.org> wrote,
istringstream(AWord(line)) >> day;


The istringstream constructed is a nameless temporary value.
Operator>> requires binding a non-constant reference to it.
This is prohibited by a silly rule. Make it not a temporary.

istringstream parser((AWord(line)));
parser >> day;

Jul 22 '05 #4
Hi David,

David Harmon <so****@netcom.com> writes:
On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates
<ya***@ieee.org> wrote,
istringstream(AWord(line)) >> day;


The istringstream constructed is a nameless temporary value.
Operator>> requires binding a non-constant reference to it.
This is prohibited by a silly rule. Make it not a temporary.

istringstream parser((AWord(line)));
parser >> day;


Yes, I had discovered that this works, but it's trashy. I shouldn't
have to create these temporary variables all over the place.

What is this "silly rule" you speak of? That inserters for
nameless temporary variables require non-constant references?
Can you expound? Is that in the ANSI/ISO C++ spec?
--
% Randy Yates % "So now it's getting late,
%% Fuquay-Varina, NC % and those who hesitate
%%% 919-577-9882 % got no one..."
%%%% <ya***@ieee.org> % 'Waterfall', *Face The Music*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #5
David Harmon wrote:
On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates
<ya***@ieee.org> wrote,
istringstream(AWord(line)) >> day;


The istringstream constructed is a nameless temporary value.
Operator>> requires binding a non-constant reference to it.
This is prohibited by a silly rule. Make it not a temporary.

istringstream parser((AWord(line)));
parser >> day;


I would agree with this analysis if operator>> were a non-member in this case.
But the int extractor is a member function, so there should be no trouble
invoking it on a temporary. Try making day a string and note the difference.

The original code is correct as far as it goes.

Jonathan
Jul 22 '05 #6
"Jonathan Turkanis" <te******@kangaroologic.com> writes:
David Harmon wrote:
On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates
<ya***@ieee.org> wrote,
istringstream(AWord(line)) >> day;


The istringstream constructed is a nameless temporary value.
Operator>> requires binding a non-constant reference to it.
This is prohibited by a silly rule. Make it not a temporary.

istringstream parser((AWord(line)));
parser >> day;


I would agree with this analysis if operator>> were a non-member in this case.
But the int extractor is a member function, so there should be no trouble
invoking it on a temporary. Try making day a string and note the difference.


There is no difference - same error.

Note that I also get the exact same error if I rename the function AWord() to anything
non-existent, e.g., "istringstream(AVeryLongRidiculousWord(line)) >> day;" gives
me the same error.
--
% Randy Yates % "My Shangri-la has gone away, fading like
%% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
%%% 919-577-9882 %
%%%% <ya***@ieee.org> % 'Shangri-La', *A New World Record*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #7
David Harmon <so****@netcom.com> writes:
[...]


Hi David,

Can you please explain what you mean by

The istringstream constructed is a nameless temporary value.
Operator>> requires binding a non-constant reference to it.

? I don't understand what you mean by "binding."
--
% Randy Yates % "The dreamer, the unwoken fool -
%% Fuquay-Varina, NC % in dreams, no pain will kiss the brow..."
%%% 919-577-9882 %
%%%% <ya***@ieee.org> % 'Eldorado Overture', *Eldorado*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #8
Randy Yates wrote:
"Jonathan Turkanis" <te******@kangaroologic.com> writes:
David Harmon wrote:
The istringstream constructed is a nameless temporary value.
Operator>> requires binding a non-constant reference to it.
This is prohibited by a silly rule. Make it not a temporary.

istringstream parser((AWord(line)));
parser >> day;


I would agree with this analysis if operator>> were a non-member in
this case. But the int extractor is a member function, so there
should be no trouble invoking it on a temporary. Try making day a
string and note the difference.


There is no difference - same error.


Okay, I reproduced the error on gcc 3.2. Your code (when corrected as I
indicated in my original reply) compiles fine on GCC 3.4 and other compilers. It
would have helped if you said what version of gcc you were using, since you are
using an old version.

gcc 3.2 seems to be interpretting istringstream(AWord(line)) as a function
declaration here.

The difference I was referring to is the difference between

bool MyFunction(string &line)
{
int day;
istringstream("hello") >> day;
return 0;
}

and

bool MyFunction(string &line)
{
string day;
istringstream("hello") >> day;
return 0;
}

The second should fail to compile for the reason that David mentioned. The first
is okay, however, since the int extractor is a member function of std::istream.

Jonathan
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Samuele Armondi | last post: by
3 posts views Thread by bml | last post: by
1 post views Thread by Donald Canton | last post: by
7 posts views Thread by Luther Baker | last post: by
6 posts views Thread by JustSomeGuy | last post: by
6 posts views Thread by James Aguilar | last post: by
11 posts views Thread by icanoop | last post: by
14 posts views Thread by sharmaharish | last post: by
reply views Thread by leo001 | last post: by

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.