Template class type

I'm getting unresolved link errors when I attempt to compile, I really don't have any idea where to go from here:

dNode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef DNODE_H
#define DNODE_H
template <class T>
class dNode {
private:
  T data;
  dNode<T>* next;
public:
  dNode(T);
  virtual ~dNode(); // base class destructor must be virtual

  template <class T> friend class dStack; // allosw dStack for private member access
  friend class dQueue; // allosw dQueue for private member access
};

#endif


dStack.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
#ifndef DSTACK_H
#define DSTACK_H

#include <iostream>
#include <new>
using namespace std;

#include "dNode.h"
template <class T>
class dStack {
private:
    dNode<T>* top_node;
public:

    class StackEmptyException {
    }; // empty inner typename for exception handling

    dStack();
    virtual ~dStack();
    void push(T);
    void pop();
    T top();
    bool isEmpty();
    bool isFull();
};

#endif


dNode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "dNode.h"

// constructor: create a new Node with d as data
template <class T>
dNode<T>::dNode(T data) {
  this->data = data;
  next = 0;
}

template <class T>
dNode<T>::~dNode() {
}



dStack.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
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
#include <cstdlib>
using namespace std;
#include "dStack.h"

// constructor: new stack is created. top_node is null.
template <class T>
dStack<T>::dStack() {
    top_node = NULL;
}

// destructor: free all the memory allocated for the stack
template <class T>
dStack<T>::~dStack() {
    while (!isEmpty())
        pop();
}

// push a data onto the stack
template <class T>
void dStack<T>::push(T data) {
    try {
        dNode<T>* newNode = new dNode(data);
        newNode->next = top_node;
        top_node = newNode;
    } catch (bad_alloc &e) {
        cout << "memory allocation exception: " << e.what() << endl;
        exit(1);
    }
}

// pop the data from the stack
template <class T>
void dStack<T>::pop() {
    if (isEmpty())
        throw StackEmptyException();

    dNode<T>* discard = top_node;
    top_node = top_node->next;
    delete discard;
}

// read the data on the top of the stack
template <class T>
T dStack<T>::top() {
    if (isEmpty())
        throw StackEmptyException();

    return top_node->data;

}

// is stack empty?
template <class T>
bool dStack<T>::isEmpty() {
    if (top_node == NULL) return true;
    else return false;
}

// is stack full? 
template <class T>
bool dStack<T>::isFull() {
    return false; // never, unless memory is full
}


main.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
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

#include <iostream>
using namespace std;

#include "dStack.h"

void stack_test();

int main() {

    stack_test();

    return 0;
}

void stack_test() {
    dStack <double> s1;

    // Stack empty test
    if (s1.isEmpty()) {
        cout << "s1 is empty at the beginning." << endl;
    } else {
        cout << "s1 must be empty. Something's wrong!" << endl;
    }

    s1.push(1.1);
    s1.push(2.2);
    s1.push(3.3);
    s1.push(4.4);
    s1.push(5.5);

    // pop() test: reverses the items
    cout << "Expected: 5.5 4.4 3.3 2.2 1.1 -->" << endl;
    for (int i = 0; i < 5; i++) {
        cout << s1.top() << endl;
        s1.pop();
    }

    // Stack empty test
    if (s1.isEmpty()) {
        cout << "s1 is empty after five pop() calls." << endl;
    } else {
        cout << "s1 must be full. Something's wrong!" << endl;
    }

    // StackEmptyException test
    cout << "Expected: StackEmptyException --> ";
    try {
        s1.pop();
    } catch (dStack<double>::StackEmptyException) {
        cout << "Exception: cannot pop, stack is empty" << endl;
    }

}



ERROR:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall dStack<double>::dStack<double>(void)" (??0?$dStack@N@@QAE@XZ) referenced in function "void __cdecl stack_test(void)" (?stack_test@@YAXXZ)main.obj prog09
Error 2 error LNK2019: unresolved external symbol "public: virtual __thiscall dStack<double>::~dStack<double>(void)" (??1?$dStack@N@@UAE@XZ) referenced in function __catch$?stack_test@@YAXXZ$0 main.obj prog09
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall dStack<double>::push(double)" (?push@?$dStack@N@@QAEXN@Z) referenced in function "void __cdecl stack_test(void)" (?stack_test@@YAXXZ) main.obj prog09
Error 4 error LNK2019: unresolved external symbol "public: void __thiscall dStack<double>::pop(void)" (?pop@?$dStack@N@@QAEXXZ) referenced in function "void __cdecl stack_test(void)" (?stack_test@@YAXXZ) main.obj prog09
Error 5 error LNK2019: unresolved external symbol "public: double __thiscall dStack<double>::top(void)" (?top@?$dStack@N@@QAENXZ) referenced in function "void __cdecl stack_test(void)" (?stack_test@@YAXXZ) main.obj prog09
Error 6 error LNK2019: unresolved external symbol "public: bool __thiscall dStack<double>::isEmpty(void)" (?isEmpty@?$dStack@N@@QAE_NXZ) referenced in function "void __cdecl stack_test(void)" (?stack_test@@YAXXZ) main.obj prog09

A header shall contain all definitions of a template class.
I'm confused, I have declared template class before all class types.
Definitions, the function body.
You can't separate templates in a cpp

(actually you can http://www.cplusplus.com/articles/1C75fSEw/ )
Topic archived. No new replies allowed.