![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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);
}
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 Nagarajan G |
| Forum Sponsor | ||
|
|
|
|||
|
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);
}
|
|
|||
|
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' Thanks nagarajan G |