|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
I'm writing a linked list program, and I'm using templates to specify which type of data the linked list will hold. In order to let other programs use my linked list, I need a header for the classes and functions. I must be declaring the headers wrong or implementing them wrong, because my compiler is giving me a crapload of errors.
My header linkedlist.h looks something like this Code:
#ifndef LINKEDLIST_H #define LINKEDLIST_H template <class E> class Link; template <class E> class LinkedList; template <class E> //retrieves first link Link<E> LinkedList::first(void); //Gives me error: " 'template<class E> struct LinkedList' used without template parameters template <class E> //add link to end of list void LinkedList::add(Link<E> *l); //same error ... ... #endif /* LINKEDLIST_H */ Code:
#include "linkedlist.h"
template <class E>
class Link {
public:
E *data;
Link<E> *next;
Link(void) {
}
Link(E *data) {
this.data = data;
}
};
template <class E>
class LinkedList {
public:
const Link<E> *const head ();
Link<E> *const last;
LinkedList(void) {
last = head;
}
LinkedList(Link<E> l) {
this();
add(l);
}
LinkedList(E o) {
this(new Link<E>(o));
}
Link<E>* first(void) {
return head->next;
}
void add(Link<E> *l) {
last->next = l;
last = l;
}
...
...
}
|
| Sponsored Links |
|
|
|
|||
|
At that point Link and LinkedList are not yet defined. In order to return a value, of that type, it needs to know the definition (to know how big it is, among other things). I don't suppose you were trying to return pointers instead?
|
|
|||
|
Originally I was trying to return pointers, but the compiler gave me another error. For instance, when I had
Code:
*Link<E> LinkedList::first(void); edit ops, turns out i wrote that wrong. Should have been Code:
Link<E> *LinkedList::first(void); Last edited by hellochar; 07-15-2008 at 05:05 PM. |
|
|||
|
Nevermind, I've figured it out. Had to use LinkedList<E>::LinkedList(). And I redeclared class LinkedList in my .cpp file, when I really just had to implement the prototypes.
|
| Sponsored Links |
|
|
|
|||
|
Just a helpful hint you don't really need - instead of using the old #ifndef/#define trick for forcing the file to include only once, just use #pragma once before any of your code in the header file.
|
|
|||
|
Quote:
![]() |
|
|||
|
True, but if you're compiling on your own computer and not someone else's, it shouldn't really matter.
|
![]() |
| Tags |
| class, error, linked list, prototype, template |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| using Template function | Chinmoy | C Tutorials | 4 | 04-03-2008 05:16 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |