c++链表编译错误
C++ Linked List Compilation Error
我有以下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函数没有定义,这是有意义的,因为它们不匹配。因此,已经做出的修复将修复这两个编译错误。
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译