调用对象类型 'ListNode *' 不是函数或函数指针

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

本文关键字:函数 指针 ListNode 对象 类型 调用      更新时间:2023-10-16

第一个函数中的第 8 行:

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

以下是.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;
}

这是 .h 中的代码

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

错误仅发生在第一个函数中。 但在第二个函数中运行良好。我尝试将局部变量虚拟人和 prev 更改为另一个名称。但它总是报告错误。我真的不知道是什么原因导致错误。请提供详细信息,我将不胜感激。

next 之后删除括号。在 for 循环中..它是一个属性,但用括号,编译器认为它是一个方法/函数