Friend function can't able to access private member

Am trying to write table object into file. Here's the source code

.hpp file

class Table
{
private:
int table_no;
std::string table_type;
bool engaged;
std::time_t start_time;
double total_sec;
public:
void set_table_no(int);
void set_table_type(std::string);
void set_engaged(bool);
void set_start_time(std::time_t);
void set_total_sec(double);
void reset_customer_details();

int get_table_no();
std::string get_table_type();
bool get_engaged();
std::time_t get_start_time();
double get_total_sec();

friend std::istream & operator >> ( std::istream &, Table &);
friend std::ostream & operator << ( std::ostream &, const Table &);
};



.cpp file

std::ifstream & operator >> (std::ifstream & in , Table &tab)
{
in >> tab.table_no;
in >> tab.table_type;
return in;
}

std::ofstream & operator << (std::ofstream & out, const Table &tab)
{
out << tab.table_no;
out << tab.table_type;
return out;
}

When i compile the above code i get the following error...

table.hpp: In function ‘std::ifstream& operator>>(std::ifstream&, Table&)’:
table.hpp:19:7: error: ‘int Table::table_no’ is private
table.cpp:91:12: error: within this context
table.hpp:20:15: error: ‘std::string Table::table_type’ is private
table.cpp:92:12: error: within this context
table.hpp: In function ‘std::ofstream& operator<<(std::ofstream&, const Table&)’:
table.hpp:19:7: error: ‘int Table::table_no’ is private
table.cpp:98:13: error: within this context
table.hpp:20:15: error: ‘std::string Table::table_type’ is private
table.cpp:99:13: error: within this context


Please help me to fix this error.. Thanks in advance
Classic game: spot the difference.
1
2
friend std:: istream & operator >> (std:: istream &    , Table &   );
       std::ifstream & operator >> (std::ifstream & in , Table &tab)
Last edited on
Thanks for the reply... I fixed it.
any one can tell me, How we can inherit function using friend function ?
Hi MiiNiPaa..
i can't write the programme of object oriented programming by operator overloading...
teacher has given me the assignment of operator overloading using string, in which we have to use (+,-,<,> and = operators)..so in (+) operator we have to add the two names like joseph+sam= joseph sam.. and in (-) operator we have to minus the names like adam - sam= dm, means only we have to minus the same letters..and in other operator we have to just assign like adam=sam and adam< sam...i hope u will understand what i am trying to say...please help me if u can.. thank you...thisis my ID plz contact me on it...awais4823@gmail.com or awaiskhan1k@yahoo.com
Last edited on
Topic archived. No new replies allowed.