error: no matching function for call to ‘ope::ope()
error: no matching function for call to ‘ope::ope()
#include <iostream>
using namespace std;
class ope
{
private: int real, imag;
public:
ope(int r, int i){
real=r;
imag=i;
}
ope operator + (ope const &obj) //operator overloading
{ ope temp;
temp.imag = imag + obj.imag;
temp.real = real + obj.real;
return temp;
}
void show()
{
cout << "Result : " << real << " + i" << imag << endl;//print complex numbers
}
};
int main()
{
ope f1(2,5) , f2(7,6);
ope f3 = f1+f2;
f3.show();
return 0;
}
我是编程新手,我尝试使用运算符重载但出现此错误有人可以帮助我吗?在此代码中,我尝试使用运算符重载来打印复数。
行
ope temp;
需要一个无参构造函数(a.k.a默认构造函数),但是ope
只有一个需要两个参数的构造函数。你不妨在这里使用参数化构造函数:
ope operator + (ope const &obj) //operator overloading
{
ope temp(real + obj.real, imag + obj.imag);
return temp;
}
... 或定义默认构造函数
class ope {
// ...
public:
ope(): real(0), imag(0) {}
// ...
#include <iostream>
using namespace std;
class ope
{
private: int real, imag;
public:
ope(int r, int i){
real=r;
imag=i;
}
ope operator + (ope const &obj) //operator overloading
{ ope temp;
temp.imag = imag + obj.imag;
temp.real = real + obj.real;
return temp;
}
void show()
{
cout << "Result : " << real << " + i" << imag << endl;//print complex numbers
}
};
int main()
{
ope f1(2,5) , f2(7,6);
ope f3 = f1+f2;
f3.show();
return 0;
}
我是编程新手,我尝试使用运算符重载但出现此错误有人可以帮助我吗?在此代码中,我尝试使用运算符重载来打印复数。
行
ope temp;
需要一个无参构造函数(a.k.a默认构造函数),但是ope
只有一个需要两个参数的构造函数。你不妨在这里使用参数化构造函数:
ope operator + (ope const &obj) //operator overloading
{
ope temp(real + obj.real, imag + obj.imag);
return temp;
}
... 或定义默认构造函数
class ope {
// ...
public:
ope(): real(0), imag(0) {}
// ...