<< and >> operator problem

1. `std::istream& operator>>(std::istream&, frac&, char&, frac&)' must take exactly two arguments
2.33 `ch' undeclared (first use this function)
3.39 redefinition of `std::ostream& operator<<(std::ostream&, frac&)'
4.40 `ch' undeclared (first use this function)
5.43 `k' undeclared (first use this function)
6.43`f' undeclared (first use this function)
i take those error
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cstdlib>
#include <iostream>

using namespace std;
/////////////////////////////////////////
class frac
{
      public:
              float pay;
              char ch;
              float payda;
      public:
             frac(): pay(0.0),payda(0.0)
             { }
             
             friend ostream& operator << (ostream& s,frac& k);
             friend istream& operator << (istream& s,frac& k);
};
//---------------------------------------
ostream& operator << (ostream& s,frac& k)
{
         s << k.pay << "/" << k.payda;
         return s;
}
//--------------------------------------
istream& operator >> (istream& s,frac& k,char& op,frac& f)
{
         cout << "1.Frac"; 
         s >> k.pay >> k.ch >> k.payda;
         cout << "2.Frac";
         s >> f.pay >> f.ch >> f.payda;
        
        op = ch; 
        
         return s;
}

ostream& operator << (ostream& s,frac& h)
{
         switch(ch)
         {
                   case '+':
                        h.pay = k.pay + f.pay;
                        h.payda = k.payda + f.payda;
                        break;
                   case '-':
                        h.pay = k.pay- f.pay;
                        h.payda = k.payda - f.payda;
                        break;
                   case '*':
                          h.pay = k.pay * f.pay;
                          h.payda = k.payda * f.payda;
                          break;
                   case '/':
                            h.pay = k.pay / f.pay;
                            h.payda = k.payda / f.payda;
                            
             }
             
         s << "Sonuc:" << endl << h.pay << "/" << h.payda << endl;
         
         return s;
}

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
I think these error messages are self explanatory:
`std::istream& operator>>(std::istream&, frac&, char&, frac&)' must take exactly two arguments
You are declaring it like this: istream& operator >> (istream& s,frac& k,char& op,frac& f)
But being a binary operator ( a >> b ), it can have only two parameters

`ch' undeclared (first use this function)
'ch' is a member of 'frac' but you are using as a variable not related to the class

redefinition of `std::ostream& operator<<(std::ostream&, frac&)'
1
2
3
4
5
6
7
8
9
10
11
12
//Line 20:
ostream& operator << (ostream& s,frac& k)
{
         s << k.pay << "/" << k.payda;
         return s;
}
//Line 38
ostream& operator << (ostream& s,frac& h)
{
         switch(ch)
         {
            


`k' undeclared (first use this function) `f' undeclared (first use this function)
You are not declaring those:
1
2
3
4
5
6
7
8
ostream& operator << (ostream& s,frac& h)
{
         switch(ch)
         {
                   case '+':
                        h.pay = k.pay + f.pay; // Where are they from?
                        h.payda = k.payda + f.payda;
                        break;


thank u.i solve it.That's OK?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdlib>
#include <iostream>

using namespace std;
/////////////////////////////////////////
class frac
{
      public:
              float pay;
              char ch;
              float payda;
      public:
             frac(float p,float pd): pay(p),payda(pd)
             { }
             
             frac(): pay(0.0),payda(0.0)
             { }
             
             friend ostream& operator << (ostream& s,frac& k);
             friend istream& operator << (istream& s,frac& k);
             
             frac operator + (frac& k)
             {
                  float tempp = pay + k.pay;
                  float tempd = payda + k.payda;
                  
                  return frac(tempp,tempd);
             }
             /////////////////////////////////7
               frac operator - (frac& k)
             {
                  float tempp = pay - k.pay;
                  float tempd = payda - k.payda;
                  
                  return frac(tempp,tempd);
             }
            //////////////////////////////////////
              frac operator * (frac& k)
             {
                  float tempp = pay * k.pay;
                  float tempd = payda * k.payda;
                  
                  return frac(tempp,tempd);
             } 
             //////////////////////////////////
               frac operator / (frac& k)
             {
                  float tempp = pay / k.pay;
                  float tempd = payda / k.payda;
                  
                  return frac(tempp,tempd);
             }   
              
};
//---------------------------------------

istream& operator >> (istream& s,frac& k)
{
          char ch;
         s >> k.pay >> ch >> k.payda;
      
         return s;
}

ostream& operator << (ostream& s,frac& h)
{       
         s << "Result:" << endl << h.pay << "/" << h.payda << endl;
         return s;
}

int main(int argc, char *argv[])
{
    frac s1,s2,s3;
    char ch;
    
    cout << "Enter fractions: ";
    cin >> s1 >>ch >> s2;
  
    switch(ch)
    {
              case '+':
                   s3 = s1 + s2;
                   break;
              case '-':
                   s3 = s1 - s2;
                   break;
              case '*':
                    s3 = s1 * s2;
                    break;
              case '/':
                   s3 = s1 / s2;
    }
        
      cout << s3 ;                        
                   
                   
                   
                   
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
It seems OK,
It would be a good practice adding some 'const'
eg:
1
2
3
frac operator + (const frac& k) const;

ostream& operator << (ostream& s, const frac& k);

Last edited on
oh yes.That's ok. and discoverable .thank u again
Topic archived. No new replies allowed.