Linked Lists

How should I write my my_list.cpp to work with my header file?

my_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <list>
#include <string>
#include <cassert>
#include "my_list.h"
using namespace std;

my_list::my_list();  // no-arg constructor 
my_list::my_list(const my_list & l); // copy constructor

my_list::~my_list(); // destructor

   // operations
bool my_list::empty() const;
int my_list::size() const;
T & my_list::back() const;
T & my_list::front() const;
void my_list::push_front(const T & x);
void my_list::push_back(const T & x);
void my_list::pop_front();
void my_list::pop_back();
iterator my_list::begin() const;
iterator my_list::end() const;
void my_list::insert(iterator pos, const T & x); // insert x before pos
void my_list::erase(iterator pos);


my_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef MY_LIST_H
#define MY_LIST_H

// my_list.h  - a doubly-linked list

#include <algorithm>
using namespace std;

// forward declaration of classes defined in this header
template <class T> class my_list;
template <class T> class my_link;
template <class T> class my_list_iterator;

template <class T> 
class my_list
{
public:
   typedef T value_type;
   typedef my_list_iterator<T> iterator;

   // constructors
   my_list();  // no-arg constructor 
   my_list(const my_list & l); // copy constructor

   ~my_list(); // destructor

   // operations
   bool empty() const;
   int size() const;
   T & back() const;
   T & front() const;
   void push_front(const T & x);
   void push_back(const T & x);
   void pop_front();
   void pop_back();
   iterator begin() const;
   iterator end() const;
   void insert(iterator pos, const T & x); // insert x before pos
   void erase(iterator pos);

protected:
   my_link<T> * first_link;
   my_link<T> * last_link;
   unsigned int my_size;
};

template <class T> 
class my_link 
{
private:
   my_link(const T & x);

   T x;
   my_link<T> * next_link;
   my_link<T> * prev_link;

   friend class my_list<T>;
   friend class my_list_iterator<T>;
};

template <class T> class my_list_iterator 
{
public:
   typedef my_list_iterator<T> iterator;

   // constructor
   my_list_iterator(my_link<T> * current_link); 

   // operators
   T & operator*();  // dereferencing operator
   void operator=(const iterator & rhs); 
   bool operator==(const iterator & rhs) const;
   bool operator!=(const iterator & rhs) const;
   iterator & operator++();  // pre-increment, ex. ++it
   iterator operator++(int); // post-increment, ex. it++
   iterator & operator--();  // pre-decrement
   iterator operator--(int); // post-decrement

protected:
   my_link<T> * current_link;
   friend class my_list<T>;
};


#include "my_list.cpp"

#endif

hi
you should only include the header file in the .cpp file.
then write the implementation of your function
if you don't know how
ask.
In the .cpp file, instead of the semicolons on the functions, use {} and put the function bodies in there.

and as alaa sam says, remove line 85 from the .h file.
You have a template class then you must implement everything within that class. No .cpp file
Topic archived. No new replies allowed.