473,545 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istringstream class question

Why does this:

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

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

istringstream(A Word(line)) >> day;
}

produce this:

cd e:\rr\thirdedit ion\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::MyFun ction(std::stri ng&)':
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 2078
Randy Yates wrote:
Why does this:

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

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

istringstream(A Word(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::MyFun ction(string &line)
{
int day;

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

produce this:

cd e:\rr\thirdedit ion\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::MyFun ction(std::stri ng&)':
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::istringstr eam or put "using std::istringstr eam;" 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**********@Ho me.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(A Word(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(l ine)));
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(A Word(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(l ine)));
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(A Word(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(l ine)));
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******@kanga roologic.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(A Word(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(l ine)));
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( AVeryLongRidicu lousWord(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******@kanga roologic.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(l ine)));
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(A Word(line)) as a function
declaration here.

The difference I was referring to is the difference between

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

and

bool MyFunction(stri ng &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
3419
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 allocated memory correctly? Or am I missing something glaringly simple? Thanks in advance, S. Armondi std::istringstream** ArgStream;
3
4719
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 string"); cout << ist.str() << endl;
1
3223
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 thought I followed his instructions on page 118 correctly, but I can't get it to compile without syntax errors in the get_token() function. The whole...
7
748
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 encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't...
4
2315
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 data. The data comes in string lines like "OREBlegQ 14854 731.818" which need to be split into a string, int and double when stored in the class....
6
2360
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 unmodified... However when I try to pass it fn(istringstream s) // doesn't compile but fn(istringstream & s) // does.
6
2984
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
2885
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
8890
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) { istringstream(s); T val; is >val;
0
7420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6003
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.