c++链表编译错误

C++ Linked List Compilation Error

本文关键字:错误 编译 链表 c++      更新时间:2023-10-16

我有以下cpp文件和h文件。这些都是老师给我们的例子,但我不想编译。我一直得到这个错误:

linked_list.cpp:50:6: error: prototype for ‘void     LinkedList::remove(LinkedListNode*, int*)’ does not match any in class     ‘LinkedList’
 void LinkedList::remove(LinkedListNode *node, int *return_value) {
      ^
In file included from linked_list.cpp:1:0:
linked_list.h:31:7: error: candidate is: void      LinkedList::remove(LinkedListNode*)
  void remove(LinkedListNode *node);
       ^
下面是代码:
#include "linked_list.h"
LinkedList::LinkedList() {
    this->start = NULL;
    this->end = NULL;
}
LinkedList::~LinkedList() {
    LinkedListNode *current = this->start;
    while (current != NULL) {
        LinkedListNode *temp = current;
        current = current->next;
        delete temp;
    }
}
void LinkedList::push(int val) {
    LinkedListNode *new_node = new LinkedListNode();
    new_node->value = val;
    new_node->next = NULL;
    new_node->prev = this->end;
    if (this->end != NULL) {
        this->end->next = new_node;
    } else {
        this->start = new_node;
    }
    this->end = new_node;
}
int LinkedList::peek() {
    if (this->end != NULL) {
        return this->end->value;
    }
    return -1;
}
int LinkedList::pop() {
    if (this->end != NULL) {
        int val = this->end->value;
        remove(this->end);
        return val;
    }
    return -1;
}
void LinkedList::remove(LinkedListNode *node, int *return_value) {
    if (this->start == node) {
        this->start = node->next;
    } else if (node->prev != NULL) {
        node->prev->next = node->next;
    }
    if (this->end == node) {
        this->end = node->prev;
    } else if (node->next != NULL) {
        node->next->prev = node->prev;
    }
    delete node;
}

头文件:

#include <cstdlib>
using namespace std;
struct LinkedListNode
{
    LinkedListNode *prev;
    LinkedListNode *next;
    int value;
};
class LinkedList
{
 public:
   LinkedList();
    ~LinkedList();
    void push(int val);
    bool insert_at(int val, int idx);
    int peek();
    int pop();
    int retrieve_at(int idx);
    bool remove_at(int idx);
    LinkedListNode *find(int val);
private:
    LinkedListNode *start;
    LinkedListNode *end;
    void remove(LinkedListNode *node);
};

下面是主文件:

#include <cstdlib>
#include <iostream>
#include "linked_list.h"
using namespace std;
int main()
{
    LinkedList list;
    list.push(5);
    cout << list.peek() << endl;
    cout << list.pop() << endl;
    cout << list.pop() << endl;
    return 0;
}

这真的很简单。如果你读一下错误,你就能知道哪里出了问题,它甚至会告诉你哪里出了问题。查看声明函数remove的头文件。然后查看定义函数remove的cpp文件。你应该看到,当函数被声明时,它又有一个int*类型的形参return_value。在函数的定义中缺少这个参数。您只需要将参数添加到cpp文件中的定义中(或从声明中删除它)

对于这个错误,

linked_list.cpp:50:6: error: prototype for void
LinkedList::remove(LinkedListNode*, int*) '不匹配类中的任何一个' LinkedList '无效LinkedList::删除(LinkedListNode *节点,int* return_value) {

问题在于.cpp(代码)中定义的remove函数与头文件(.h文件)中声明的任何函数都不匹配。要解决这个问题,请将头文件(.h文件)中的remove函数更改为以下内容:

//    void remove(LinkedListNode *node); // Wrong
    void remove(LinkedListNode *node, int*);
在.cpp文件中,将pop()函数更改为:
int LinkedList::pop() {
    if (this->end != NULL) {
//        int val = this->end->value; // Wrong 
//        remove(this->end); // Wrong
        int val; // new
        remove(this->end, &val); // new

下一个编译错误以不同的方式抱怨相同的问题。它抱怨在头文件中声明的remove函数没有定义,这是有意义的,因为它们不匹配。因此,已经做出的修复将修复这两个编译错误。