Typename AND Class in one template?
May 21, 2011 at 6:26am UTC
I've been working on a Linked List class in a header file and I'm confused as to how I should proceed with this... so far, I can create a list of a data type, such as int, double, or float, but I can't figure out how I could change the template so that it could accept both data types and objects of classes. I could change the template to template< class T >, but then it gives me a syntax error when I try to create a list of INTs or other data types. What can I do? Here's my code:
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
/**
* This header file was designed to make creation and modification of linked lists easier.
* Author - packetpirate
* Last Update - 05/18/2011 11:19 PM
**/
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
using std::cout;
using std::endl;
// Begin linklist namespace.
namespace linklist
{
// Begin node structure.
template < typename T >
struct node {
T data; // Contains data of specified type.
node<T> *link; // Contains link to next node. NULL if this node is last in the list.
};
// End node structure.
// Begin LinkedList class declaration.
template < typename T >
class LinkedList {
public :
LinkedList(); // Creates a new blank list.
LinkedList(node<T> *start); // Creates a new list with a specified first node.
~LinkedList();
// Functions for appending the list.
void add(node<T> *next); // Adds a node to the list.
void add(T nextData); // Adds a new node to the list.
// Get and Set functions for the list.
T get(int pos);
void set(int pos, T newData);
// Size-based functions.
int length();
bool isEmpty();
// Position-based functions.
T begin(); // Returns the data of the first node in the list.
T end(); // Returns the data of the last node in the list.
// Sort function/s.
void sort(bool asc);
void swap(node<T> *i, node<T> *j);
// Print functions.
void printList();
void printElem(int pos);
private :
node<T> *first; // The first node in the list.
node<T> *last; // The last node in the list.
int size; // Size of the list.
};
// End LinkedList class declaration.
// Begin LinkedList class definitions.
// Begin Constructors & Destructors
template < typename T >
LinkedList<T>::LinkedList() {
first = NULL;
last = NULL;
size = 0;
}
template < typename T >
LinkedList<T>::LinkedList(node<T> *start) {
if (start->link != NULL) {
first = start;
node<T> *current = start;
int count = 0;
for (current = start;current != NULL;current = current->link) {
count++;
if (current->link == NULL) {
last = current;
size = count;
}
}
} else {
first = start;
last = start;
size = 1;
}
}
template < typename T >
LinkedList<T>::~LinkedList() {
}
// End Constructors & Destructors
// Begin list modifying functions.
template < typename T >
void LinkedList<T>::add(node<T> *next) {
if (first == NULL) {
first = next;
last = next;
last->link = NULL;
size++;
} else {
last->link = next;
last = next;
last->link = NULL;
size++;
}
}
template < typename T >
void LinkedList<T>::add(T nextData) {
node<T> *next = new node<T>;
next->data = nextData;
next->link = NULL;
if (first == NULL) {
first = next;
last = next;
last->link = NULL;
size++;
} else {
last->link = next;
last = next;
last->link = NULL;
size++;
}
}
// End list modifying functions.
// Begin get/set functions.
template < typename T >
T LinkedList<T>::get(int pos) {
if (pos > size) {
cout << "That position is out of bounds." << endl;
} else {
int i = 0;
node<T> *current;
for (current = first;current != NULL;current = current->link) {
if (i == pos) {
return current->data;
}
i++;
}
}
return 0;
}
template < typename T >
void LinkedList<T>::set(int pos, T newData) {
if (pos > size) {
cout << "That position is out of bounds." << endl;
} else {
int i = 0;
node<T> *current;
for (current = first;current != NULL;current = current->link) {
if (i == pos) {
current->data = newData;
}
i++;
}
}
}
// End get/set functions.
// Begin size-based functions.
template < typename T >
int LinkedList<T>::length() {
return size;
}
template < typename T >
bool LinkedList<T>::isEmpty() {
return (first == NULL);
}
// End size-based functions.
// Begin position-based functions.
template < typename T >
T LinkedList<T>::begin() {
if (!isEmpty()) {
return first->data;
} else {
return NULL;
}
}
template < typename T >
T LinkedList<T>::end() {
if (!isEmpty()) {
return last->data;
} else {
return NULL;
}
}
// End position-based functions.
// Begin sort function/s.
template < typename T >
void LinkedList<T>::sort(bool asc) {
if (!isEmpty()) {
node<T> *i;
node<T> *j;
for (i = first;i != NULL;i = i->link) {
for (j = i->link;j != NULL;j = j->link) {
if (asc) {
if (j->data < i->data) {
swap(i, j);
}
} else {
if (j->data > i->data) {
swap(i,j);
}
}
}
}
}
}
template < typename T >
void LinkedList<T>::swap(node<T> *i, node<T> *j) {
T tempData = i->data;
i->data = j->data;
j->data = tempData;
}
// End sort function/s.
// Begin print functions.
template < typename T >
void LinkedList<T>::printList() {
if (!isEmpty()) {
node<T> *current;
for (current = first;current != NULL;current = current->link) {
cout << current->data << " " ;
}
cout << endl;
} else {
cout << "The list is empty." << endl;
}
}
template < typename T >
void LinkedList<T>::printElem(int pos) {
if (pos > size) {
cout << "That position is out of bounds." << endl;
} else {
int i = 0;
node<T> *current;
for (current = first;current != NULL;current = current->link) {
if (i == pos) {
cout << current->data << endl;
break ;
}
i++;
}
}
}
// End print functions.
// End LinkedList class definitions.
}
// End linklist namespace.
#endif
May 21, 2011 at 7:33am UTC
There should be no such problems either way. What error did you get and what was in your main?
May 21, 2011 at 7:50am UTC
I think the OP may be confused bewteen the use of the terms typename and class when applied to template parameters.
May 21, 2011 at 8:31am UTC
The template
and class
keywords are equivalent in every way when inside a template parameter list declaration.
May 21, 2011 at 1:50pm UTC
When it was set to < class T > and I tried creating the list like this:
LinkedList<int > *myList = new LinkedList<int >();
...it said that "typename is not allowed".
May 21, 2011 at 1:59pm UTC
Try compiling this
1 2 3 4 5 6 7 8 9
template <class T>
class C{
T* c;
};
int main(){
C<int > *cc = new C<int >();
return 0;
}
Topic archived. No new replies allowed.