Error on operator overload

I tried a tutorial about operator overload
On line 16 (nComplex sum;), I had the following error:

no matching function for call to 'nComplex::nComplex()'

How am I suppose to fix this?
Thank you in advance

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
#include <iostream>
using namespace std;

class nComplex{
private:
	int nReal;
	int nImg;

public:
	nComplex(int a, int b){
		this->nReal = a;
		this->nImg = b;
	}

	nComplex operator + (const nComplex &x){
		nComplex sum;
		sum.nReal = this->nReal + x.nReal;
		sum.nImg = this->nImg + x.nImg;
		return sum;
	}

	void print(){
		cout << this->nReal << " & " << this->nImg << endl;
	}

};

int main() {

	nComplex o1(3,4);
	nComplex o2(7,1);
	nComplex o3 = o1 + o2;
	o3.print();
	return 0;
}
Since you have a user-defined ctor the compiler doesn't create a default ctor for you.
The solution is to create one.
Another solution would be:
1
2
3
4
5
6
nComplex operator + (const nComplex &x){
		nComplex sum = *this;
		sum.nReal += x.nReal;
		sum.nImg += x.nImg;
		return sum;
	}
or
1
2
3
4
5
6
7
8
nComplex operator + (const nComplex &x){
		nComplex sum
		{
		  this->nReal + x.nReal,
		  this->nImg + x.nImg
		};
		return sum;
	}
It's working now.
Thank you so much
Topic archived. No new replies allowed.