user3737386
user3737386

Reputation: 11

Call object type 'ListNode *' is not a function or function pointer

The line 8 in the first function:

ListNode* prev = &dummy;
Have a error: Call object type 'ListNode *' is not a function or function pointer

The following is the code in .cpp :

ListNode* deleteDuplicatesII(ListNode* head)
{
    if (head == nullptr) return nullptr;

    ListNode dummy(-1);
    dummy.next = head;

    ListNode* prev = &dummy; // Error here: Call object type 'ListNode *' is not a function or function pointer
    for (ListNode* cur = prev->next(), *next = cur->next; next != nullptr;)
    {
        if (cur->value == next->value)
        {
            while (next != nullptr && cur->value == next->value)
            {
                cur->next = next->next;
                delete next;
                next = cur->next;
            }
            prev->next = cur->next;
            delete cur;
            cur = prev->next;    // now the cur == next
            if (cur == nullptr) break;
            else next = cur->next; // maybe cur is nullptr
        }
        else
        {
            prev = cur;
            cur = next;
            next = next->next;
        }
    }

    return dummy.next;
}




ListNode* Solution::reverseLinkedList(ListNode* head, int m, int n)
{
    ListNode dummy(-1);
    dummy.next = head;
    head = &dummy;       // It works well
    for (int i = 0; i < m - 1; i++) {
        head = head->next;
    }

    ListNode* prev = head->next;
    ListNode* cur = prev->next;
    for (int i = m; i < n; i++, cur = prev->next) {
        prev->next = cur->next;
        cur->next = head->next;
        head->next = cur;
    }

    return dummy.next;
}

This is the code in .h

struct ListNode
{
    ListNode* next;
    int value;
    ListNode(int v): value(v), next(nullptr){}
};

The error only happened in the first function. but works well in the second function. And I have tried to change the local variable dummy and prev to another name. But it always report a error. I really don't know what causes the error. Please provide details and I will appreciate it.

Upvotes: 1

Views: 175

Answers (1)

Pieter21
Pieter21

Reputation: 1845

remove parenthesis after next.. in the for loop.. it is a property, but with parenthesis, compiler thinks it is a method/function

Upvotes: 2

Related Questions