Link to home
Start Free TrialLog in
Avatar of puckerhoop
puckerhoop

asked on

Class vector output to file

I am able to load data from a file into a class vector.
I can not figure out how to filestream it out to a file at the end of my program (with a CLASS vector).

    ofstream out_stream;      
    out_stream.open("apt_data.txt");

    if ( !out_stream)
        {
        cout << "File could not be opened" << endl;
        exit (1);
        }

//PIPE OUT DATA...
for (int i = 0; i < record.size(); i++)      (e1)
   out_stream << (record[i]);                (e2)

        out_stream.close( );

I am getting the following errors:
(e1) Comparing signed and unsigned values
(e2) 'operator<<' not implemented in type 'ofstream' for arguments of type 'Apartment' (Apartment is my class)
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bcladd
bcladd

(1) Or, even better, change i's type from int to vector<Apartment>::size_type

-bcl
Avatar of puckerhoop

ASKER

On this first set of code i am getting the following error:
'Apartment ::<<(ofstream&,const Apartment &)' is not a member of 'Apartment'

    ofstream& Apartment::operator<<(ofstream &stream, const Apartment& a)
    {
    stream << the_buildNum << the_aptNum << the_aptType << the_view << the_highFloor << the_rentalStatus << the_cust_name << the_rent_paid;
    return stream;
    }

I have done as suggested and placed this code within the class, in public:
    friend ofstream& operator<<(ofstream &stream, const Apartment& a);

Maybe I am calling it wrong?

    for (unsigned int i = 1; i < record.size(); i++)
        {
        out_stream << the_buildNum << the_aptNum << the_aptType << the_view << the_highFloor << the_rentalStatus << the_cust_name << the_rent_paid;
        }
        out_stream.close( );
Avatar of Axter
FYI:

In the above operator class, the class prefix should be removed from the implementation.
Example:

ofstream& operator<<(ofstream &stream, const Apartment& a)
{
   stream << a.m1 << a.m2;   // ... where m1, m2 - class members of simple types (like int, char etc.)

  return stream;
}

You can have an operator that is global or one that is a class method.
Since the class is declaring the operator as a friend, that means you need a global function implementation.

With operators, it's usually better to create a global function then it is to create a class method.
>>On this first set of code i am getting the following error:

Remove "Apartment::" from the function.
The concept works, but my compiler does not like it.  I did pull 'Apartment', as it is a friend not a member.

My compiler:  Borland 5.0
does not not like the function declaration of:

  ofstream& operator<<(ofstream &stream, const Apartment& a);
    {
    stream << the_buildNum << the_aptNum << the_aptType << the_view << the_highFloor << the_rentalStatus << the_cust_name << the_rent_paid;
    return stream;
    }

On the function header, it gives me 2 errors of:
"Improper use of typedef 'ofstream'"
and
"Undefined symbol 'stream'"
and
"Expression syntax"

then it can't access the private member variables (ie. the_buildNum).

any other ideas?
ofstream& operator<<(ofstream &stream, const Apartment& a);

remove ; in the end
 ofstream& operator<<(ofstream &stream, const Apartment& a)
    {
  stream << Apartment.the_buildNum << Apartment.the_aptType << ... etc
  return stream;
    }
I am adding the following into my public class:
ofstream& operator<<(ofstream& stream, const Apartment& a);

And the following in my .cpp file:
ofstream& Apartment::operator<<(ofstream& stream, const Apartment& a)
{
stream << Apartment.the_buildNum << Apartment.the_aptType << Apartment.the_aptType << Apartment.the_view << Apartment.the_highFloor << Apartment.the_rentalStatus << Apartment.the_cust_name << Apartment.PastDue << Apartment.the_rent_paid << Apartment.rentDue << Apartment.rent;
  return stream;
}

error on both:
'Apartment::operator <<ofstream(ofstream&,constApartment&)'must be declared with one parameter

Additional error on function:
Improper use of typedef 'Apartment'
I have changed "Apartment." in the stream to "a."
   this elminates the improper use error

BUT adds:
 'operator<<' not implemented in type 'ofstream' for arguments of type 'int'