Solved

istrstream issues

Posted on 2001-06-27
8
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

617 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