代码之家  ›  专栏  ›  技术社区  ›  Stiven Choking

C++中的“操作符*”没有匹配

  •  0
  • Stiven Choking  · 技术社区  · 4 年前

    class X {
         
       public:
          double getLength(void) {
             return length;
          }
          void setLength( double len ) {
             length = len;
          } 
          
          // Overload + operator to add two Box objects.
         X operator*(X temp){
             X temp1;
             temp1.length = this->length * temp1.length;
             return temp1;
          }
          
          X operator*(int num){
             X temp1;
             temp1.length = this->length * num;
             return temp1; 
          }
          
         X operator+(X temp){ 
             X temp1;
             temp1.length = this->length + temp1.length;
             return temp1; 
          }
          
       private:
          double length;      // Length of a box
          
    };
    
    // Main function for the program
    int main() {
       X ob1;                // Declare Box1 of type Box
       X ob2;                // Declare Box2 of type Box
       X ob3;                // Declare Box3 of type Box
       double result = 0.0;     // Store the volume of a box here
     
       ob2.setLength(6.0);  
       ob3.setLength(12.0);  
     
       ob1 = ob2 + 2*ob3;
       ob1 = ob2*2 + ob3;
       ob1 = (ob2 + 2) *ob3;
     
       cout << "length of Box  : " << ob1.getLength() <<endl;
    
       return 0;
    }
    

    main.cpp: In function 'int main()':
    main.cpp:48:17: error: no match for 'operator*' (operand types are 'int' and 'X')
        ob1 = ob2 + 2*ob3;
                    ~^~~~
    main.cpp:50:15: error: no match for 'operator+' (operand types are 'X' and 'int')
        ob1 = (ob2 + 2) *ob3;
               ~~~~^~~
    main.cpp:27:8: note: candidate: 'X X::operator+(X)'
          X operator+(X temp){
    

    2 回复  |  直到 4 年前
        1
  •  3
  •   Some programmer dude    4 年前

    ob3 * 2:;  // Fine: Calls ob3.operator*(2)
    2 * ob3;   // Not fine, there's no operator* function which takes an int on the left-hand side
    

    您可以使用

    X operator*(int a, X b)
    {
        return b * a;  // Calls b.operator*(a)
    }
    

    int

    2 * ob3;  // Fine: Calls operator+(2, ob3)
    

    this operator overloading canonical implementaiton reference operator*= operator* *=

        2
  •  2
  •   463035818_is_not_an_ai    4 年前

    operator* int * X operator+ X + int X * int X * X X + X

    const

    #include <iostream>
    
    class X {         
       public:
          X(double length=0.0) : length(length) {}   // <- converting constructor
          double getLength() const {             // <- const !!!
             return length;
          }
          void setLength(double len) {
             length = len;
          } 
       private:
          double length;      // Length of a box          
    };
    
    X operator*(const X& a,const X& b){
        return {a.getLength() * b.getLength()};
    }
         
    X operator+(const X& a,const X& b){ 
        return {a.getLength() + b.getLength()}; 
    }
    
    int main() {
       X ob2{6.0};
       X ob3{12.0};
       X ob3;
     
       ob1 = ob2 + 2*ob3;
       ob1 = ob2*2 + ob3;
       ob1 = (ob2 + 2) *ob3;
     
       std::cout << "length of Box  : " << ob1.getLength() << std::endl;
    }