473,698 Members | 2,025 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 2088
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
3426
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
4736
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
3237
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 program follows with the four syntax errors flagged with the text "// SYNTAX ERROR". Thanks in...
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 matter whether or not I use the std::ios::binary flag when opening the istringstream or the...
4
2325
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. Can anyone help? This is what i have so far: /* Begin Code */ #include <sstream>
6
2369
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
2993
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
2906
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
8909
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
8598
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9152
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9014
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6515
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4358
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
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
2
2320
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1995
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.