Reputation:
i'm attempting to implement three methods currently a get_first(), get_last() and print_node(). get_first() will return the head of a list, get_last() the tail, and print_node() will just print the data field of a node sent to it. im trying to implement but continually getting pointer errors for any changes that i make.
here's my node.h header:
class Node
{
private:
int data;
Node *next;
Node *prev;
friend class LinkedList;
};
class LinkedList
{
private:
Node *head;
Node *tail;
public:
LinkedList();
~LinkedList();
bool empty();
void insert_left(int v);
void insert_right(int v);
Node* get_first();
Node* get_last();
void print_list();
void print_node(Node *n);
void remove_left();
void remove_right();
protected:
void add(Node *v, int d);
void remove(Node *v);
};
here are the relevant portions of my list.cpp class implementation file:
#include <iostream>
#include "node.h"
using namespace std;
LinkedList :: LinkedList()
{
head = new Node;
tail = new Node;
head->next = tail;
tail->prev = head;
}
LinkedList :: ~LinkedList()
{
while(!empty())
{
remove_left();
}
delete head;
delete tail;
}
void LinkedList :: add(Node *v, int d)
{
Node *u = new Node;
u->data = d;
u->next = v;
u->prev = v->prev;
v->prev->next = v->prev = u;
}
void LinkedList :: print_list()
{
Node *tmp = head;
while(tmp != NULL)
{
cout << tmp->data << endl;
tmp = tmp->next;
}
}
void LinkedList :: print_node(Node *n)
{
Node *tmp = n;
cout << tmp->data << endl;
}
Node LinkedList :: get_first()
{
return head;
}
Node LinkedList :: get_last()
{
return tail;
}
finally here's my main function in a file called main.cpp:
#include <cstdlib>
#include <iostream>
#include "list.cpp"
using namespace std;
int main(int agrc, char **argv)
{
LinkedList *l = new LinkedList();
//LinkedList *m = new LinkedList();
l->insert_left(200);
l->insert_left(700);
l->insert_left(300);
Node *temp = l->get_first();
//l->print_list();
l->print_node(temp);
delete l;
return 0;
}
here's the current error output:
g++ main.cpp -o main
In file included from main.cpp:3:
list.cpp:85: error: prototype for ‘Node LinkedList::get_first()’ does not match any in class ‘LinkedList’
node.h:24: error: candidate is: Node* LinkedList::get_first()
list.cpp:90: error: prototype for ‘Node LinkedList::get_last()’ does not match any in class ‘LinkedList’
node.h:25: error: candidate is: Node* LinkedList::get_last()
make: *** [main] Error 1
i'm not sure of the exact changes to make but i think it has to do with how i'm returning the head in the get_first() and last() functions. Please excuse the length of the post.
Upvotes: 0
Views: 903
Reputation: 311146
Data members head
and tail
are defined as
Node *head;
Node *tail;
that is they are pointers to Node. So if any function returns either head
or tail
then its return type has to be Node *
So these member function definitions
Node LinkedList :: get_first()
{
return head;
}
Node LinkedList :: get_last()
{
return tail;
}
are wrong. They return head and tail but have no the return type Node *
and their definitions do not coinside with theor declarations in the class.
Also the constructor definition is wrong. It shoild look as
LinkedList :: LinkedList() : head( nullptr ), tail( nullptr )
{
}
In this case member function empty should be declared as
bool empty() const;
and defined as
bool empty() const { return ( head == nullptr ); }
Upvotes: 0
Reputation: 985
You are returning Node* in function declaration but in definition you have Node as the return type. Use this
Node* LinkedList :: get_first()
{
return head;
}
Node* LinkedList :: get_last()
{
return tail;
}
Upvotes: 2