Thread: << and >> overloading

  1. #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

  2. #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.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    8
    wow im an idiot, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading << and >>
    By MMu in forum C++ Programming
    Replies: 1
    Last Post: 04-21-2008, 06:49 AM
  2. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 04:33 PM
  4. Overloading the << and >> operators
    By WebmasterMattD in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2002, 05:22 PM
  5. istream >> overloading
    By wazza13 in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2002, 10:56 PM