![]() |
| | #1 |
| Registered User Join Date: Nov 2003
Posts: 8
| << and >> overloading 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;
}
"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 inaccessiblec.owners=new Owner[c.numOwners+1]; ----------^" etc Anything I am forgetting? I cant see it if i did Thanks, Arm |
| Armatura is offline | |
| | #2 |
| Cheesy Poofs! 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 | |
| | #3 |
| Registered User Join Date: Nov 2003
Posts: 8
| wow im an idiot, thanks |
| Armatura is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |