istringstream class question

R

Randy Yates

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'
%%%% <[email protected]> % *El Dorado*, Electric Light Orchestra
http://home.earthlink.net/~yatescr
 
J

Jonathan Turkanis

Randy said:
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
 
B

Bob Hairgrove

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?
 
D

David Harmon

On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates
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;
 
R

Randy Yates

Hi David,

David Harmon said:
On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates


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..."
%%%% <[email protected]> % 'Waterfall', *Face The Music*, ELO
http://home.earthlink.net/~yatescr
 
J

Jonathan Turkanis

David said:
On Sun, 02 Jan 2005 04:47:06 GMT in comp.lang.c++, Randy Yates


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
 
R

Randy Yates

Jonathan Turkanis said:
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 %
%%%% <[email protected]> % 'Shangri-La', *A New World Record*, ELO
http://home.earthlink.net/~yatescr
 
R

Randy Yates

David Harmon said:

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 %
%%%% <[email protected]> % 'Eldorado Overture', *Eldorado*, ELO
http://home.earthlink.net/~yatescr
 
J

Jonathan Turkanis

Randy said:
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
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top