Solved

istrstream issues

Posted on 2001-06-27
8
238 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
Independent Software Vendors: 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!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 45
mixing C++ & C# in Vis Studio 2013 7 283
I could not build boost code, 10 117
Need some help with design 17 48
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

710 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