【问题标题】:Operator Overloading C++ for + operator运算符重载 C++ for + 运算符
【发布时间】:2021-10-19 05:34:34
【问题描述】:

我目前正在学习如何在 C++ 中进行运算符重载,我在网上找到了一些在下面运行时有效的代码。

class Complex {
 private:
  int real, imag;

 public:
  Complex(int r = 0, int i = 0) {
    real = r;
    imag = i;
  }

  // This is automatically called when '+' is used with
  // between two Complex objects
  Complex operator+(Complex const &obj) {
    Complex res;
    res.real = real + obj.real;
    res.imag = imag + obj.imag;
    return res;
  }
  void print() { cout << real << " + i" << imag << endl; }
};

int main() {
  Complex c1(10, 5), c2(2, 4);
  Complex c3 = c1 + c2;  // An example call to "operator+"
  c3.print();
}

但是,当我尝试类似的结构时,我收到以下错误:未找到默认构造函数和

class Chicken {
 private:
  int i;
  int k;
  int s;
  int total = 0;

 public:
  Chicken(int index, int kentucky, int sign) {
    i = index;
    k = kentucky;
    s = sign;
  }
  Chicken operator+(Chicken obj) {
    Chicken Chicky;
    Chicky.total = i + obj.i;
    return Chicky;
  }
};

int main() {
  Chicken P(1, 2, 3);
  Chicken U(2, 2, 2);
  Chicken W = P + U;

  cout << W;
}

enter image description here

【问题讨论】:

    标签: c++ operator-overloading


    【解决方案1】:

    Below 是更正的示例。首先,您收到错误是因为在 operator+ 内部您默认构造了一个 Chicken 类型的对象,但您的类没有任何默认构造函数。 解决方案是添加默认构造函数。

    #include <iostream>
    class Chicken{
        //needed for cout<<W; to work
        friend std::ostream& operator<<(std::ostream& os, const Chicken &rhs);
        private:
          int i = 0;
          int k = 0;
          int s = 0;
          int total = 0; 
    
        public:
    
        
    
        //default constructor 
        Chicken() = default;
        
        
        //use constructor initializer list instead of assigning inside the body
        Chicken(int index, int kentucky, int sign): i(index), k(kentucky), s(sign){
    
            //i = index;
            //k = kentucky;
            //s = sign;
        }
        Chicken operator + (Chicken obj){
            Chicken Chicky;//this needs/uses default constructor
            Chicky.total = i + obj.i;
            return Chicky;
        }
       
    };
    std::ostream& operator<<(std::ostream& os, const Chicken &rhs)
    {
        os << rhs.i << rhs.k << rhs.s << rhs.total ;
        return os;
    }
    int main(){
        
       
        
        Chicken P(1,2,3); //this uses parameterized constructor
        Chicken U(2,2,2); //this uses parameterized constructor
        Chicken W = P+U;  //this uses overloaded operator+
        
        std::cout << W;   //this uses oveloaded operator<<
        
    }
     
    

    其次,您使用了cout&lt;&lt;W; 语句,但没有重载operator&lt;&lt;。要使std::cout&lt;&lt; W; 工作,您需要重载operator&lt;&lt;,如我在上面的代码中所示。

    您可以查看工作示例here

    第三,您可以/应该使用构造函数初始化列表,而不是如上所示在构造函数体内赋值。

    【讨论】:

    • 但是为什么示例代码有参数化构造函数,但是operate+中创建的obj没有引用默认构造函数而是参数化构造函数呢?
    • @Chicken 您在operator+ 的主体中创建的名为chicky 的对象使用默认构造函数。虽然您在 main() 中创建的名为 PU 的对象是使用参数化构造函数创建的,因为您正在显式传递参数。查看我在创建对象的每个语句之后添加的 cmets。
    • 谢谢!但我实际上是在询问 Class Complex 代码,在该代码中,obj Complex res;好像是在调用参数化构造函数,这是为什么呢?
    • @Chicken 参数化构造函数为其参数提供默认值,因此可以用作默认构造函数。 Complex(int i = 0, int r = 0) { ... } 是一个构造函数,您可以但不必为ir 提供值。
    • @Chicken 这是因为该构造函数具有默认参数(对于它的所有参数)。因此可以使用相同的构造函数而不传递任何参数。因此,您调用的参数化构造函数实际上被视为默认构造函数。默认构造函数的定义如下: 默认构造函数是没有参数的构造函数,或者如果有参数,则所有参数都有默认值。
    猜你喜欢
    • 2012-11-26
    • 2016-02-19
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多