please help me with operator << and >>

I have just learnt programming C++. I typed a program about operator on Borland C++ 502. When I compiled, the warning is: 'complex::real' is not accessible and 'complex::image' is not accessible. I don't know why. Please show me my errors. I really thanks everyone!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  /* definte operator >> vs << */
#include <iostream.h>
#include <conio.h>
#include <math.h>
 
class complex
{
private:
    float real,image;
friend ostream & operator<<(ostream &o, complex &b);
friend istream & operator<<(istream &i, complex &b);
 
};
 
ostream & operator<<(ostream &os, complex &b)
{
    os<<b.real<<(b.image>=0?'+':'-')<<"j*"<<fabs(b.image)<<endl;
   return os;
}
 
istream & operator>>(istream &is, complex &b)
{
    cout<<"Real : ";
   is>>b.real;
   cout<<"Image : ";
   is>>b.image;
   return is;
}
 
void main(void)
{
    clrscr();
   cout<<"Create a complex a\n";
   complex a;
   cin>>a;
   cout<<"Create a complex b\n";
   complex b;
   cin>>b;
   cout<<"Print 2 complex \n";
   cout<<"a = "<<a;
   cout<<"b = "<<b;
   getch();
}
Last edited on
Your function on line 21 doesn't match the declaration on line 11, so the compiler then tries to create a new function that isn't a friend of the class. Careful with copy-paste :)

In other words, change line 11 to operator>>, not operator<<.
Last edited on
Thanks Ganado very much! I fixed this problem. I typed myself, not copy paste. I know that want to learn programming just code, code and code... I'm trying do that. Thank you again!
Hello anup30. I am learning form my book. This is a material of an university in my country. What can I do for you?
I think the point he was getting at was the following:
If you're doing it for a university, just stick with what they're telling you to do for now... but you should know that your code is outdated and would not compile on modern compilers. (The two main things being that main should return int, not void, and <iostream> should not be appended with .h, and also that conio.h is not a standard header)
Last edited on
Topic archived. No new replies allowed.