Solved

Class vector output to file

Posted on 2003-11-02
10
344 Views
Last Modified: 2010-04-02
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)
0
Comment
Question by:puckerhoop
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
Comment Utility
1) Change i type from int to unsigned int

2) Add operator<< to Apartment class:

class Apartment
{
    ...
    friend ofstream& operator<<(ofstream &stream, const Apartment& a);

    ...
};

ofstream& Apartment::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;
}
0
 
LVL 11

Expert Comment

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

-bcl
0
 

Author Comment

by:puckerhoop
Comment Utility
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( );
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
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.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>On this first set of code i am getting the following error:

Remove "Apartment::" from the function.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:puckerhoop
Comment Utility
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?
0
 
LVL 48

Expert Comment

by:AlexFM
Comment Utility
ofstream& operator<<(ofstream &stream, const Apartment& a);

remove ; in the end
0
 

Expert Comment

by:blackdev
Comment Utility
 ofstream& operator<<(ofstream &stream, const Apartment& a)
    {
  stream << Apartment.the_buildNum << Apartment.the_aptType << ... etc
  return stream;
    }
0
 

Author Comment

by:puckerhoop
Comment Utility
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'
0
 

Author Comment

by:puckerhoop
Comment Utility
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'
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

771 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

11 Experts available now in Live!

Get 1:1 Help Now