Solved

Class vector output to file

Posted on 2003-11-02
10
355 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
ID: 9665959
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
ID: 9665972
(1) Or, even better, change i's type from int to vector<Apartment>::size_type

-bcl
0
 

Author Comment

by:puckerhoop
ID: 9666071
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 30

Expert Comment

by:Axter
ID: 9666081
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
ID: 9666086
>>On this first set of code i am getting the following error:

Remove "Apartment::" from the function.
0
 

Author Comment

by:puckerhoop
ID: 9666160
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
ID: 9669252
ofstream& operator<<(ofstream &stream, const Apartment& a);

remove ; in the end
0
 

Expert Comment

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

Author Comment

by:puckerhoop
ID: 9670609
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
ID: 9670656
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
C++ assignment question 7 236
sorting efficency of sorting algorithm 30 121
gdb doesn't stop on breakpoint 2 94
boost::uuid crashes 17 11
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

856 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