If you used a combination of C++ overloaded operators, I have a question about g++ compilation error.

Asked 2 years ago, Updated 2 years ago, 84 views

During class, there was a task to implement a program that implemented complex numbers as classes and used operator overloading to spin simple tests. Among them, there was a test case where the global function overloaded **(multiple) operator and *++(rear increment/decrease operator)** were used together, and Visual Studio succeeded in compiling and ran normally A compilation error occurred when compiling with g++ in a Linux environment. I looked around through Google, including stack overflow, but there was no example like the test case, so I posted a question here. Below is a new code created by extracting the error part of the source code.

#include <iostream>
using namespace std;

class Complex{
private:
    int real;
    int imag;

public:
    //constructor
    Complex(int r = 0, int i = 0) : real(r), imag(i) {}
    Complex(const Complex& copy) : real(copy.real), imag(copy.imag) {}

    //accessor
    int getReal()const { return real; }
    int getImag()const { return imag; }

    //global
    friend Complex operator*(int value, Complex& num);  

    //overloaded binary operator
    Complex operator*(const Complex& num)const;
    Complex operator*(const int value)const;

    //overload assignment operator
    Complex& operator=(const Complex& num);
    Complex& operator=(const int value);

    //overloaded unary operators
    Complex& operator++();
    Complex operator++(int);
    Complex& operator--();
    Complex operator--(int);
};

//global
Complex operator*(int value, Complex& num){
    return  num*value;
}

//overloaded binary operator
Complex Complex::operator*(const Complex& num)const{
    int newReal = (this->real)*num.getReal() - (this->imag)*num.getImag();
    int newImag = (this->real)*num.getImag() + (this->imag)*num.getReal();
    return Complex(newReal, newImag);
}
Complex Complex::operator*(const int value)const{
    int newReal = (this->real)*value;
    int newImag = (this->imag)*value;
    return Complex(newReal, newImag);
}

//overload assignment operator
Complex& Complex::operator=(const Complex& num){
    this->real = num.getReal();
    this->imag = num.getImag();
    return *this;
}
Complex& Complex::operator=(const int value){
    this->real = value;
    this->imag = 0;
    return *this;
}

//overloaded unary operators
Complex& Complex::operator++(){
    real += 1;
    return *this;
}
Complex Complex::operator++(int){
    Complex temp(*this);
    real += 1;
    return temp;
}
Complex& Complex::operator--(){
    real -= 1;
    return *this;
}
Complex Complex::operator--(int){
    Complex temp(*this);
    real -= 1;
    return temp;
}

//main
int main(){
    Complex c0, c1(1);

    c0 = (++c1) * 3; //Case_1
    c0 = 3 * (++c1); //Case_2
    c0 = (c1++) * 2; //Case_3
    c0 = 3 * (c1++); //Case_4 => Error
    return 0;
}

In the Main above, Case_1~3 are compiled normally Case_4 outputs the following error message:

I wonder why the error occurs in Case_4 in g++. Thank you for reading the long article (__)

c++ g++ linux

2022-09-22 13:18

1 Answers

Write const for the second factor in the overloaded part as a global function. VS falls over this, so it becomes a compile. In case of operator overloading, this error occurs if the object in the r-value is not specified as const. Make the following corrections and then compile them~

    //global
    friend Complex operator*(int value, Complex const& num);  

    ...

    //global
    Complex operator*(int value, Complex const& num){
        return  num*value;
    }

Below is the full code.

#include <iostream>
using namespace std;

class Complex{
private:
    int real;
    int imag;

public:
    //constructor
    Complex(int r = 0, int i = 0) : real(r), imag(i) {}
    Complex(const Complex& copy) : real(copy.real), imag(copy.imag) {}

    //accessor
    int getReal()const { return real; }
    int getImag()const { return imag; }

    //global
    friend Complex operator*(int value, Complex const& num);  

    //overloaded binary operator
    Complex operator*(const Complex& num)const;
    Complex operator*(const int value)const;

    //overload assignment operator
    Complex& operator=(const Complex& num);
    Complex& operator=(const int value);

    //overloaded unary operators
    Complex& operator++();
    Complex operator++(int);
    Complex& operator--();
    Complex operator--(int);
};

//global
Complex operator*(int value, Complex const& num){
    return  num*value;
}

//overloaded binary operator
Complex Complex::operator*(const Complex& num)const{
    int newReal = (this->real)*num.getReal() - (this->imag)*num.getImag();
    int newImag = (this->real)*num.getImag() + (this->imag)*num.getReal();
    return Complex(newReal, newImag);
}
Complex Complex::operator*(const int value)const{
    int newReal = (this->real)*value;
    int newImag = (this->imag)*value;
    return Complex(newReal, newImag);
}

//overload assignment operator
Complex& Complex::operator=(const Complex& num){
    this->real = num.getReal();
    this->imag = num.getImag();
    return *this;
}
Complex& Complex::operator=(const int value){
    this->real = value;
    this->imag = 0;
    return *this;
}

//overloaded unary operators
Complex& Complex::operator++(){
    real += 1;
    return *this;
}
Complex Complex::operator++(int){
    Complex temp(*this);
    real += 1;
    return temp;
}
Complex& Complex::operator--(){
    real -= 1;
    return *this;
}
Complex Complex::operator--(int){
    Complex temp(*this);
    real -= 1;
    return temp;
}

//main
int main(){
    Complex c0, c1(1);

    c0 = (++c1) * 3; //Case_1
    c0 = 3 * (++c1); //Case_2
    c0 = (c1++) * 2; //Case_3
    c0 = 3 * (c1++); //Case_4 => Error
    return 0;
}


2022-09-22 13:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.