Hi,
I have a base class and derived a class from the base class, i want to print & read the data for the object created for the derived class,so i have overloaded both the << and >> operators and also have done the foward declaration.
Below is the code snippet,
#include <iostream>
class Line; // Forward Declaration for friend
class Point {
private:
int x,y;
public:
virtual void draw() ;
friend std::ostream& operator<< ( std::ostream& , const Line&);
friend std::istream& operator>> ( std::istream& , Line&);
};
class Line : public Point {
private:
int color;
public :
void draw () { std::cout<< "Drawing a Line" << std::endl; }
friend std::ostream& operator<< ( std::ostream& , const Line&);
friend std::istream& operator>> ( std::istream& , Line&);
};
inline
std::ostream& operator<< ( std::ostream &os , const Line &out ) {
os<<"Line: [" << out.x << "," << out.y << " ," << out.color << " ]" << std::endl;
return os;
}
inline
std::istream& operator>> ( std::istream &is , const Line &in ) {
std::cout<<"Line x , y , color info: " << std::endl;
is >> in.x >> in.y >> in.color;
return is;
}
int main(int argc, char *aargv) {
Line new_line;
std::cin>>new_line;
std::cout<<new_line;
return(0);
}
Am using g++ and on compiling i get the below errors, how to fix this
g++ friends.cpp
friends.cpp: In function `std::istream& operator>>(std::istream&, const Line&) ':
friends.cpp:8: error: `int Point::x' is private
friends.cpp:34: error: within this context
friends.cpp:8: error: `int Point::y' is private
friends.cpp:34: error: within this context
friends.cpp:18: error: `int Line::color' is private
friends.cpp:34: error: within this context
Thanks
Nagarajan G