0

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);
        }

northernSage
  • 1,264
  • 1
  • 6
  • 18
Lev Marder
  • 19
  • 6
  • 3
    Totally unrelated: You may find your time spent with C++ to be a lot easier if you learn [the Rule of Three (and friends)](https://en.cppreference.com/w/cpp/language/rule_of_three) and [use `new` (and pointers) a lot less often](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Mar 28 '19 at 22:04
  • 2
    For terminology, what this code calls a `List` is more commonly called a `Node`. A `List` would have code that manages `Node` objects to maintain the list. – Pete Becker Mar 28 '19 at 22:33

3 Answers3

4

According to [over.oper]/6:

An operator function shall either be a non-static member function or be a non-member function that has at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.

Therefore, your operator+ is illegal, but the compiler has not diagnosed this because the template was not instantiated (the standard allows but does not require the compiler to issue a diagnostic in such a case). When you attempted to add the two List<int> pointers, according to [over.match.oper]/1:

If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator ...

Therefore, the compiler did not instantiate your operator+ template, but instead simply issued an error because the built-in + operator cannot operate on two pointers.

You do not need to overload any operators to merge lists. You can simply write an ordinary function to do that instead.

Brian Bi
  • 91,815
  • 8
  • 136
  • 249
  • Thank you for your support. It's ok, I have a function to merge them but overloading is a part of my homework and I have to somehow make this happen. Any ideas? – Lev Marder Mar 29 '19 at 08:18
0

You can't overload operator+ to take 2 pointers as input, or to return a pointer as output. It needs to take object references as input, and return a new object by value as output (which requires your class to support the Rule of 3/5, which it currently does not). Your current class design is not well-suited for supporting concatenation operations.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
0

I find the solution by changing operator+ operands from List* to simply List:

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;
}

The problem is that it is not convenient to write code like this, but whatever:

mem = *head + *bonus;

Thank everyone for your support!

Lev Marder
  • 19
  • 6