not working

closed account (SECMoG1T)
Am really puzzled by an operator overload, am in the mid of my project and just wont let me progress further, I can't really figure where the problem is, the code seems okay to me.

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
  struct data
  {
    int value; 
    int copies;
    
    data ():value (0), copies (0){}
    data (int v, int c): value (v), copies (c){}
    data operator +( const data& rhs)
     {
         int val= value+rhs.value; 
         int cop= copies+rhs.copies;
          return data (val, cop);
     }

     istream& >> (istream& in)
     {
        in>> value>> copies; 
        return in;
     }
   
     data& operator = (const data& dt)
     {
       if (value==dt.value&& copies==dt.copies)
            return *this;

       else
        {
          value=dt.value; 
          copies= dt.copies; 
        }
   
         return *this;
      }
  }

 
  int main ()
  {
    data dt1, dt2, dt3;
 
    cin>> dt1; ///error here "struct data have no member cin";
    cin>> dt2; 

    dt2+=dt1; 
  }



I will appreciate your help, thank you.
hope this example help you
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
#include <iostream>
using namespace std;

class data
{
private:
	double real, imag;
public:
	data(){
		real = 0; 
		imag = 0;
	}

	data(double r, double i) {
		real = r;
		imag = i;
	}

	double getReal() const {
		return real;
	}

	double getImag() const {
		return imag;
	}

	data & operator=(const data &);
	const data operator+(const data & );
	data & operator++(void);
	data  operator++(int);

	/*friend const 
		data operator+(const data&, const data&); */

	friend ostream& operator<<(ostream& os, const data& c);

        // note: no const for the second parameter
        friend istream& operator>>(istream& is, data& c);
};

data & data::operator=(const data& c) {
	real = c.real;
	imag = c.imag;
	return *this;
}

const data data::operator+(const data& c) {
	data temp;
	temp.real = this->real + c.real;
	temp.imag = this->imag + c.imag;
	return temp;
}

//pre-increment
data & data::operator++() {
	real++;
	imag++;
	return *this;
}

//post-increment
data data::operator++(int) {
	    data temp = *this;
	    real++;
	    imag++;
	    return temp;
}


ostream& operator<<(ostream &os, const data& c) {
	os << c.real << '+' << c.imag << 'i' << endl;
	return os;
}


istream& operator>>(istream &is, data& c) {
	is >> c.real >> c.imag;
	return is;
}

int main()
{
	data c1(5,10);
	cout << "c1 = " << c1.getReal() << "+" << c1.getImag() << "i" << endl;

	cout << "Using overloaded ostream(<<) " << endl;
	cout << "c1 = " << c1 << endl;

	data c2;
	cout << "Enter two numbers: " << endl;
	cin >> c2;
	cout << "Using overloaded istream(>>) " << endl; 
	cout << "Input complex is = " << c2;

	return 0;
}
1) Line 15: this should give you an error because this is not how you overload operator
2) operator>> must be nonmember gunction, as if you will have it as member, then left hand side operand will be data argumnt, which is not how stream operations work
3) You did not define operator +=

Fixed code:
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
#include <iostream>

struct data 
{
    int value;
    int copies;

    data():value (0), copies (0) {}
    data(int v, int c): value(v), copies(c) {}
    data& operator+=(const data& rhs)
    {
        value += rhs.value;
        copies += rhs.copies;
        return *this;
    }
    data& operator = (const data& dt) = default; //Default version is fine
};

//Prefer binary functions to be nonmembers
data operator+(const data& lhs, const data& rhs)
{
    data temp(lhs);
    temp += rhs;
    return temp;
}

std::istream& operator>>(std::istream& in, data& d)
{
    in >> d.value >> d.copies;
    return in;
}

int main ()
{
    data dt1, dt2;

    std::cin >> dt1;
    std::cin >> dt2;

    dt2+=dt1;
}
closed account (SECMoG1T)
Thank you very much @minnippa and @anup for the help and the advice too, my code Is working now , great solution there, I dint know += these was an ooperator , I thought it was just an implicit combo of the assignment and + operators, operator>> must be a nonmember  I have not seen these yet btw i'll try to look it up, meanwhile I'll settle with your solution, am really glad.

Thanks again.
Topic archived. No new replies allowed.