1 year ago

#384571

test-img

Sean Zlatev

Error: invalid operands of types className<int>* and className<int>* to binary 'operator+'

I'm trying to make a class where I create a number based off of nodes linking to each other, allowing for larger integers. I overloaded the operator to allow individual nodes to add to each other as such:

template <class T>
class Number    {
public:
    Number(int bigNum = 0){
        value = bigNum;
    }
        Number operator + (Number const &num)   {
            Number* total = new Number(value + num.value);
            
            return *total;
        }
    T value;
    Number<T>* left;
    Number<T>* right;
};
//I'll just make the main function as follows:
int main() {

    Number<int> *num1 = new Number<int>(6);
    Number<int> *num2 = new Number<int>(4);
    Number<int> *total = num1 + num2;
}

I add two Numbers together here:

Number<T> *total = num1 + num2;

and got the following error:

invalid operands of types 'Number<int>*' and 'Number<int>*' to binary 'operator+'

enter image description here

c++

pointers

operator-overloading

0 Answers

Your Answer

Accepted video resources