c++ - 错误:没有匹配函数来调用‘ope::ope()

标签 c++ oop operator-overloading

 #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;

需要一个无参数的构造函数(又名默认构造函数),但是 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) {}
// ...

关于c++ - 错误:没有匹配函数来调用‘ope::ope(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67717779/

相关文章:

c++ - 重置后尝试访问指针

javascript - 减少对 JavaScript 对象方法的调用次数

c++ - 为什么重载 operator new 会改变 new[] 的行为?

php - 关于命令模式(PHP)的问题

java - 如何编辑 ObservableList 中的特定对象?

c++ - 理解 operator() 重载

c++ - 包装数组的类的转换与下标运算符重载

c++ - 模板参数默认为后一个

c++ - 如何相对于其他元素过滤 vector 元素?

c++ - PKCS7_sign 返回 null