The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to make a loop base on reading a file? JohnBalayo UNIX for Dummies Questions & Answers 3 04-02-2008 03:36 AM
How to make a loop base on reading a file? JohnBalayo HP-UX 3 04-01-2008 08:58 PM
How to make variables in script function local? alex_5161 Shell Programming and Scripting 5 03-07-2008 10:03 AM
welcome YOUR NEW FRIEND RETO retolop UNIX for Dummies Questions & Answers 0 06-13-2007 12:30 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-20-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-20-2007
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 946
Stumble this Post!
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);
}
Reply With Quote
  #3 (permalink)  
Old 11-20-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Stumble this Post!
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
Reply With Quote
  #4 (permalink)  
Old 05-29-2008
Registered User
 

Join Date: May 2008
Posts: 1
Stumble this Post!
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0