Solved

istrstream issues

Posted on 2001-06-27
8
231 Views
Last Modified: 2008-02-20
Hi - I have had an error that's been driving me nuts all day! Any help would be greatly appreciated. I have already exhausted all internet searches for help with this topic in spite of similar articles but i can't seem to get this exact instance of my error resolved.

By the way, this code compiled perfectly on unix but i am pasting it into visual c++ and this is the only error i am really getting. from what i can tell, it is a minor bug. i need the fix!

The issue at hand:

#include <strstrea.h>

FileInfo parseLine(string & line)
{
     istrstream curLine(line.c_str());  
 }

//this line gives me error C2664: '__thiscall istrstream::istrstream(char *)' : cannot convert parameter 1 from 'const char *' to 'char *'
        Conversion loses qualifiers


how can i fix this?? Thanks so much for any help.

Melissa
0
Comment
Question by:melgro
8 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Try the following:
FileInfo parseLine(string & line)
{
    istrstream curLine(line.c_str(), line.length());  
}
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Are you using the VC libraries with *.h extension?
You should use extensionless headers for the standard C++ libraries.

Try the following:

#include <strstream>

FileInfo parseLine(string & line)
{
    istrstream curLine(line.c_str());  
}
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
I forgot the using namespace.


#include <strstream>

using namespace std;

FileInfo parseLine(string & line)
{
   istrstream curLine(line.c_str());  
}

Don't mix extensionless C++ libraries with (*.h) extension C++ libraries.  You should keep them all the same.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>//this line gives me error C2664: '__thiscall
>>istrstream::istrstream(char *)' : cannot convert
>>parameter
>>1 from 'const char *' to 'char *'
>>       Conversion loses qualifiers

You'll have to do a 'const_cast' to get rid of the 'const' qualifier:

#include <strstrea.h>

FileInfo parseLine(string & line)
{
    istrstream curLine(const_cast<char*>(line.c_str()));  
}

However, the 'clean' solution would be to make a copy of the original 'string' to not mess up things:

FileInfo parseLine(string & line)
{
    string temp = line;
    istrstream curLine(const_cast<char*>(temp.c_str()));  
}

PS: Axter, I'd usually havwe used the 'Answer' button right now even though you commented on this...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:Axter
Comment Utility
Thank you jkr
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
Comment Utility
melgro, I don't think that what the compiler wants to do is what you want to do.  The compiler is trying to use the const char * that you're sending in as the buffer that future data will go into.  You probably want:
istrstream curLine;
curLine << line.c_str();

If you're trying to conform to the standard C++, you should probably move to the istringstream class (in <sstream>), but that's up to you.

Hope this helps.

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
mnashadka's advice is good.  You don't want to use strstream.  That class is a mess!   it is a depreciated class that was developed before templates were added to the language and it has many flaws--in particular its issues about the data ownership.   See the problem is that the array returnee by c_str() is owned by the string class and is of a temporary nature.  You have to be very careful about how you use it.  It may become invalidated without your're realizing it.  Furthermore this array must not be changed.  Doing so might not only corrupt the string you are using, but also might corrupt other string objects as well.  That is why the c_str() procedure returns a pointer to an array of constant characters.   But the stream class wants an array of non-constant characters.  It wants to retain ownership of this array, which in general it can't do since it is temporary in nature, and it wants to be able to change the array to store new data--which can corrupt your strings.  This is hopeless.  Using a cast to remove constance will allow the code to compile, but may allow it to then work incorrectly.  You could have unpredicatable problems.

If you switch to stringstream this will be much much easier.  Plus stringstream is much more powerful.  
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "mnashadka" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now