I'm trying to write overloaded insertion and exertion operators but the functions aren't able to access the private members of the class Money.I have "include namespace std;" and "#include " but it won't let me access the private members (dollars and cents).
ostream & operator<<(ostream &osObject, Money &right)
{
osObject << "Amount of money: $" << right.dollars << ".";
osObject << right.cents;
return osObject;
}
istream &operator>>(istream &isObject, Money &right)
{
isObject >> right.dollars;
isObject.ignore();
getline(isObject, right.cents;
return isObject;
}`
Header file:
#pragma once
class Money{
// Friends
friend ostream & operator<<(ostream, Money);
friend istream & operator>>(istream, Money);
private:
int dollars;
int cents;
void simplify();
//Some other stuff...
I'm getting errors such as: syntax error: missing ';' before '&' 'ostream' : 'friend' not permitted on date declarations missing type specifier- int assumed. C++ does not support default-int
getline(isObject, right.cents;is just a typo... - vsoftco