473,387 Members | 2,436 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,387 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 2067
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Samuele Armondi | last post by:
Hi everyone, Since istringstream objects are not assignable, I'm using the following code to allocate some dynamically. My question is: Is this the correct way of doing it? Am I deleting all the...
3
by: bml | last post by:
Could you help and answer my questions of istringstream? Thanks a lot! 1. Reuse an "istringstream" istringstream ist; ist.str("This is FIRST test string"); ist.str("This is SECOND test...
1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
4
by: dinks | last post by:
Hi I'm really new to c++ so please forgive me if this is really basic but im stuck... I am trying to make a data class that uses istringstram and overloaded << and >> operators to input and output...
6
by: JustSomeGuy | last post by:
I am passing an istringstream to a function. I want that function to get a copy of the istringstream and not a refrence to it. ie when the function returns I want the istringstream to be...
6
by: James Aguilar | last post by:
Hello all, I am trying to use an istringstream to do some input off of cin by lines. The following snippet does not work: char buf; cin.getline(buf, 90); istringstream line1(string(buf));
11
by: icanoop | last post by:
I would like to do this MyClass x; istringstream("XXX") >> x; // Works in VC++ but not GCC instead of MyClass x; istringstream iss("XXX"); iss >> x; // Works in both GCC and VC++
14
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.