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


 
Thread Tools Search this Thread
Top Forums Programming How to make a function friend to both base and derived class
# 1  
Old 11-20-2007
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,
Code:
#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
Code:
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
# 2  
Old 11-20-2007
Try the following:

Code:
#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);
}

# 3  
Old 11-20-2007
Reading enum from cin

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

Code:
#include <iostream>

using namespace std;

enum C {
 red,blue,green
};


int main () {
 C color;
 cin >> color;
 cout << color;
 return(0);
}

Code:
Compilation Error : 
 error: no match for 'operator>>' in 'std::cin >> color'

Please help me to get this working?


Thanks
nagarajan G
# 4  
Old 05-29-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Derived both lines base on pattern

Dear All, I have a requests to retrieve all lines if second line last columns meets certain criteria. Here is the output of the result Table: DSSBDW.DA_MASK_CLK_AR 120908 0 86 86 - 1934528 0 70 100 -*- Table: DSSBDW.DA_MASK_CLK_IP 310657 22030 143 185 - 5281169 7 88 77 *-* Table:... (5 Replies)
Discussion started by: ckwan123
5 Replies

2. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

3. Programming

Size of derived class, in case of multiple inheritance

Why, here the size of class 'Derived' is 8 ? class Base1 { public: virtual void f() { } }; class Base2 { public: virtual void f() { } }; class Derived : public Base1, Base2 { public: virtual void f() { } }; (1 Reply)
Discussion started by: techmonk
1 Replies

4. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

5. Programming

C++ compilation error when I use predicate friend function in the std::sort()

Hi, Can anyone tell me why the following program is giving compiler error when I use a friend function of a class as the comparison predicate for the third parameter of std::sort() algorithm? How to correct it, keep the 'friend' intact? #include <iostream> #include <vector> #include <list>... (1 Reply)
Discussion started by: royalibrahim
1 Replies

6. Programming

Base class's variables not accessible????

Hi friends, The derived class cannot access the base class's variables in my program. You can have a look at my code, I am actually using class templates. #include <iostream> using namespace std; template <class T> class Sum { friend void Check(Sum &s, T a, T b) { T x,... (2 Replies)
Discussion started by: gabam
2 Replies

7. Programming

Please help debug this friend class in C++

Please help check an old code: #include <iostream> using namespace std; template <class C> class List { //Linked list of C. template<class U> friend class ListItr; private: class ListEl { public: C val; ListEl* next; ListEl(const C& s, ListEl* n) : val(s) { next = n;} };... (0 Replies)
Discussion started by: learncode
0 Replies

8. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies
Login or Register to Ask a Question