C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-07-2003, 05:29 PM   #1
Registered User
 
Join Date: Nov 2003
Posts: 8
<< and >> overloading

I am currently writing code for a class where I need to overload the << and >> operators....now when I do so it gives me errors that the private components I'm trying to access within the class are unaccessable...

I have defined both the << and >> operator overloads as friends in the class description but still it says it cannot access the members

Is there something else I need to do? heres some snippets of code

Code:
class Store
{
      friend ostream & operator>>(ostream&, const Store &);
      friend istream & operator<<(istream&, Store &);
      public:
    .....

private:
      float revenue;
      int numOwners;
      Owner *owners;

      void copy(const Store &s);
      void free();

};



istream &operator>>(istream &in, Store &c)
{
        int i;
        in >> ws >> c.name >> c.revenue >> c.numOwners;

        c.owners=new Owner[c.numOwners+1];

        for(i=0; i<c.numOwners; i++)
        {
                c.owners[i].read(inn);
        }

        return in;
}


ostream &operator<<(ostream &out, const Store &c)
{
        int i;

        out << "Name: " << c.name <<endl;
        out << "Revenue: " <<setiosflags(ios::showpoint) << setiosflags(ios::fixed) << setprecision(2) << c.revenue << " (in Millons)" <<endl;

        out << "NumOwners: " << c.numOwners <<endl;
        out << "Owners:" <<endl;

        for(i=0; i<c.numOwners; i++)
        {
                c.owners[i].print(out);
        }

        return out;
}
and with this I get errors for in accesability like:
"cxx: Error: Store.cc, line 121: member "Store::revenue" is inaccessible
in >> ws >> c.name >> c.revenue >> c.numOwners;
--------------------------------^
cxx: Error: Store.cc, line 121: member "Store::numOwners" is inaccessible
in >> ws >> c.name >> c.revenue >> c.numOwners;
---------------------------------------------^
cxx: Error: Store.cc, line 123: member "Store:wners" is inaccessible
c.owners=new Owner[c.numOwners+1];
----------^"

etc

Anything I am forgetting? I cant see it if i did

Thanks,

Arm
Armatura is offline   Reply With Quote
Old 12-07-2003, 06:10 PM   #2
Cheesy Poofs!
 
PJYelton's Avatar
 
Join Date: Sep 2002
Location: Boulder
Posts: 1,728
You have the << and >> backwards so the compiler thinks the friends and the function implementations are different functions.
PJYelton is offline   Reply With Quote
Old 12-07-2003, 06:19 PM   #3
Registered User
 
Join Date: Nov 2003
Posts: 8
wow im an idiot, thanks
Armatura is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Overloading << and >> MMu C++ Programming 1 04-21-2008 06:49 AM
Overloading fstream's << and >> operators Bubba C++ Programming 2 04-09-2007 03:17 AM
Overloading << and >> Enahs C++ Programming 2 09-15-2005 04:33 PM
Overloading the << and >> operators WebmasterMattD C++ Programming 9 10-15-2002 05:22 PM
istream >> overloading wazza13 C++ Programming 1 05-03-2002 10:56 PM


All times are GMT -6. The time now is 05:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22