?
Solved

Class vector output to file

Posted on 2003-11-02
10
Medium Priority
?
365 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 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