Reputation: 135
I have the following cpp files and h file. These were examples given to us by the instructor but do not want to compile. I keep getting this error:
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);
^
here is the code:
#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;
}
Here is the header file:
#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);
};
here is the main file:
#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;
}
Upvotes: 0
Views: 281
Reputation: 830
For this error,
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) {
the issue is that the remove function that is defined in the .cpp (code) does not match any of those functions declared in the header (.h file). To fix, change the remove function in the header (.h file) to the following:
// void remove(LinkedListNode *node); // Wrong
void remove(LinkedListNode *node, int*);
And in the .cpp file, change the pop() function to:
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
The next compile error is complaining about the same issues but in a different way. It is complaining that the remove function declared in the header has no definition, which makes sense because they didn't match. So the fix that is already made will fix both compile errors.
Upvotes: 2
Reputation: 2952
It is really easy.. If you would just read the errors you could see what is wrong, it even tells you where. Look at the header file where the function remove
is declared. Then look at the cpp file where the function remove
is defined. You should see that when the function gets declared it has one more Parameter of type int* called return_value
. This Parameter is missing at the definition of the function. You just have to add the parameter to the definition in the cpp file ( or remove it from the declaration)
Upvotes: 2