I need to do a Linked List and overloading the '+' operator in C++. I read some articles like this , but no success. Basically, I have a simple class like above, but actually a take this error: invalid operands of types ‘MyList*’ and ‘MyList*’ to binary ‘operator+’
class MyList {
private:
int value;
MyList* next;
public:
MyList(int value);
~MyList();
MyList* operator+(MyList* list){
return new MyList(this->value + list->value);
}
};
MyList::MyList(int value) {
this->next = NULL;
this->value = value;
}
MyList::~MyList() {
cout << "Destroy" << endl;
}
int main() {
MyList *list1 = new MyList(5);
MyList* list2 = new MyList(2);
MyList* result = list1 + list2; //here this error: invalid operands of types ‘MyList*’ and ‘MyList*’ to binary ‘operator+’
cout << result;
delete list1;
delete list2;
delete result;
return 0;
}
Any suggestions?