Template, header, implementation Question
Sep 27, 2008 at 2:00am UTC
Hi all, I'm having problem separating the following codes into header file and cpp file. Can help?
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
template <typename T> class List;
template <typename T>
class Node
{
T data;
Node<T> *next, *prev;
friend class List<T>;
public :
Node(const T & D, Node<T> * N = 0, Node<T> * P = 0) : data(D), next(N), prev(P) {}
}; //End of Node class
template <typename T>
class List
{
Node<T> *first, *last;
public :
List():first(0) {}
~List()
{
Node<T> *q;
for (Node<T> *p=first; p; )
{
q=p;
p=p->next;
delete q;
}
}
void pushH(const T & D)
{
Node<T> *p = new Node<T>(D, first, 0);
if (!first)
{
p->next = 0;
first = p;
last = first;
}
else
{
first->prev = p;
first = p;
}
}
bool popH(T & D)
{
if (!first) return false ;
D = first->data;
Node<T> * q = first;
if (first == last)
{
first = 0;
last = first;
}
else
{
first = first->next;
}
delete q;
return true ;
}
void pushT(const T & D)
{
if (!first)
pushH(D);
Node<T> * p = new Node<T>(D, 0, last);
last->next = p;
last = p;
}
bool popT(T & D)
{
if (!first) return false ;
if (last == first)
D = last->data;
Node<T> *q = last;
if (last == first)
{
first = 0;
last = first;
}
else
{
Node<T> *p = last->prev;
p->next = 0;
last = p;
}
delete q;
return true ;
}
void show()
{
Node<T> *p = first;
if (p) cout << p->data;
p = p->next;
while (p)
{
cout << ' ' << p->data;
p=p->next;
}
}
}; //End of List class
Sep 27, 2008 at 2:47am UTC
I'm pretty sure all of that can be a .h file, since it's all a class except for one or two lines.
But, I still don't quite understand what you mean, but, anyway, hope that helps!
Sep 27, 2008 at 7:14am UTC
No, templates can't be seperated in .cpp and .h files. Search the board. Special cases (keyword export, explicit instantiation) were discussed.
Sep 27, 2008 at 8:29am UTC
hmm... actually i was tasked to separate the above code (see 1st post) into the a .h file along with a .cpp file.
the .h file was given (as shown below).
I need to come up with the .cpp file.
Hope this clarify my question.... really desperate for help here. T.T
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
template <typename T> class List;
template <typename T>
class Node
{
T data;
Node<T> *next, *prev;
friend class List<T>;
friend class ListIter<T>;
friend class RListIter<T>;
public :
Node(const T &, Node<T> *, Node<T> *);
};
template <typename T>
class List
{
Node<T> *first, *last;
friend class ListIter<T>;
friend class RListIter<T>;
public :
List();
~List();
void pushH(const T &);
bool popH(T &);
void pushT(const T &);
bool popT(T &);
void show();
};
Sep 29, 2008 at 7:12am UTC
Thanks guys, for all the help.
I've got my prob solved.
It's really a dumb mistake on my part. *duhz*
Sorry for wasting virtual space on this forum with my dumb question.
T.T
Topic archived. No new replies allowed.