How to make a function friend to both base and derived class

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

Try the following:

#include <iostream>

using namespace std;

class Line; // Forward Declaration for friend

class Point
{
  friend ostream& operator<< ( ostream& , const Line&);
  friend istream& operator>> ( istream& , Line&);

 private:
  int x,y;
};

class Line : public Point
{
  friend ostream& operator<< ( ostream& , const Line&);
  friend istream& operator>> ( istream& , Line&);

 private:
  int color;

 public :
   void draw () { cout<< "Drawing a Line" << endl; }
};

inline
ostream& operator<< ( ostream &os , const Line &out )
{
  os << "Line: [" << out.x << "," << out.y << " ," << out.color << " ]" << endl;
  return os;
}

inline
istream& operator>> ( istream &is , Line &in )
{
  cout << "Line x , y , color info: " << endl;
  is >> in.x >> in.y >> in.color;
  return is;
}

int
main(int argc, char *argv)
{
   Line new_line;

   cin >> new_line;
   cout << new_line;

   return(0);
}

Thanks!!
I realized that the friends are not part of the class and after moving the friend function on top of the classes it started to work.

Also i have a question how do i read an enum value from cin

#include <iostream>

using namespace std;

enum C {
 red,blue,green
};


int main () {
 C color;
 cin >> color;
 cout << color;
 return(0);
}
Compilation Error : 
 error: no match for 'operator>>' in 'std::cin >> color'

Please help me to get this working?

Thanks
nagarajan G

I'm curious to know why placing the friendship declaration on top makes a difference.
What does 'friends are not part of the class' actually means?

Thanks a lot,
thanks,
Michele