I'm getting a problem and I don't have any ideas about what is wrong. I need to overload operator +
for my class so I can merge two and more lists.
The error
Xcode keeps saying:
Invalid operands to binary expression ('List *' and 'List *')
My code
template <typename Type> class List {
public:
Type data;
List *next;
void set_head(Type d) {
data = d;
next = nullptr;
}
void int_print() {
cout << data << endl;
}
};
template <typename Type>
List<Type>* operator+ (List<Type> *head1, List<Type> *head2) {
List<Type> *tmp = head1, *headf = nullptr, *tmpf = nullptr;
tmpf = list_create_head(tmp, tmp->data);
headf = tmpf;
tmp = tmp->next;
while (tmp != nullptr) {
tmpf = list_create_continue(tmpf, tmp->data);
tmp = tmp->next;
}
tmp = head2;
while (tmp != nullptr) {
tmpf = list_create_continue(tmpf, tmp->data);
tmp = tmp->next;
}
return headf;
}
//problem occurs here:
else if ((c == 8) * (bonus != nullptr)) {
List<int> *mem = nullptr;
mem = head + bonus; //Here!
free(bonus);
cout << "Result of merging: " << endl;
tmp = mem;
while (tmp != nullptr) {
tmp->int_print();
tmp = tmp->next;
}
free(mem);
}