Solved

istrstream issues

Posted on 2001-06-27
8
234 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

863 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

27 Experts available now in Live!

Get 1:1 Help Now