In article <68c73679.0409161916.10f234e1@posting.google.com >,
dinko81@yahoo.com (dinks) wrote:
[color=blue]
> Hi I'm really new to c++ so please forgive me if this is really basic
> but im stuck... I am trying to make a data class that uses
> istringstram and overloaded << and >> operators to input and output
> data. The data comes in string lines like "OREBlegQ 14854 731.818"
> which need to be split into a string, int and double when stored in
> the class. Can anyone help?[/color]
class lab2Data {
string ID;
int inum;
double fnum;
friend ostream& operator<<( ostream&, const lab2Data& );
friend istream& operator>>( istream&, lab2Data& );
};
[color=blue]
> This is what i have so far:
>
> /* Begin Code */
>
> #include <sstream>
>
> using namespace std;
>
> class lab2Data
> {
> public:
> string *ID;[/color]
Don't use a string*, use a string.
[color=blue]
> int inum;
> double fnum;
>
> lab2Data();
> lab2Data( string *A, int B, double C );[/color]
Again, don't use a string*.
[color=blue]
> friend ostream& operator<<(ostream& osObj, const lab2Data &data);
> friend void operator<<(istringstream& isObj, const lab2Data &data);[/color]
That should be:
friend istream& operator>>( istream& isObj, lab2Data& data );
[color=blue]
> };
>
> lab2Data::lab2Data( string *A, int B, double C )
> {
> ID = A;
> inum = B;
> fnum = C;
> };
>
> lab2Data::lab2Data()
> {
> ID = 0;
> inum = 0;
> fnum = 0;
> };
>
> ostream& operator<<(ostream& osObj, const lab2Data &data)
> {
> osObj << data.ID << " " << data.inum << " " << data.fnum << "\n";
> return osObj;
> }
>
> void operator<<(istringstream &isObj, lab2Data &data)
> {
> }[/color]
That should be:
istream& operator>>( istream& isObj, lab2Data& data ) {
// put something here.
return s;
}
What do you think would happen if you simply used:
isObj >> data.ID >> data.inum >> data.fnum;
in the above? Have you tried it?
I suspect you are having all kinds of problems because of the string
pointer in the class rather than a string object.