Solved

istrstream issues

Posted on 2001-06-27
8
237 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
ID: 6233092
Try the following:
FileInfo parseLine(string & line)
{
    istrstream curLine(line.c_str(), line.length());  
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 6233112
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
ID: 6233127
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 6233353
>>//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
 
LVL 30

Expert Comment

by:Axter
ID: 6233387
Thank you jkr
0
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 6233394
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
ID: 6235389
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
ID: 6820078
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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