Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Class vector output to file

Posted on 2003-11-02
10
Medium Priority
?
370 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
[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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 750 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
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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

648 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