Undefined reference to...

I have looked everywhere for a solution to my problem, but it seems that something is wrong with my g++ linker. When I run the following batch program

1
2
3
4
5
g++-3 -c -Wall node.cpp -o Compile\node.o
g++-3 -c -Wall nodeList.cpp -o Compile\nodeList.o
g++-3 -c -Wall test.cpp -o Compile\test.o
g++-3 -o index Compile\node.o Compile\nodeList.o Compile\test.o
pause


I get this as an error (NOTE: the compiling aspect of the batch file goes without a hitch)

Compile\test.o:test.cpp:(.text+0x178): undefined reference to 'node<int>::node(int)'
Compile\test.o:test.cpp:(.text+0x199): undefined reference to 'nodeList<int>::nodeList(node<int&, int)'
...


And so on and so forth for the destructors and methods.

Now, I know that this is telling me that the prototypes are not implemented, but they are in the .cpp files (down below) are are compiled and linked together using g++. When I put the implementation of the classes in the .h files, it works flawlessly, but I know that this isn't good practice

I guess my question is, what am I doing wrong?

test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cstdlib>
#include <iostream>
#include "node.h"
#include "nodeList.h"

using namespace std;

int main(int argc, char** argv) 
{
	char temp[10];
    node<int> myNode(3);
    nodeList<int> myNodeList(myNode, 5);
    myNodeList[4]->value = 5;
	cout << myNodeList[4]->value;
	cin >> temp;
    return 0;
}


node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef NODE_H
#define	NODE_H

template <typename type>
class node
{
public:
    node();
    node(const node<type>& p);
    node(type p);
    ~node();
    node<type>* operator = (const node<type>* p);
    node<type>* operator = (const node<type>& p);
    node<type>* previous;
    node<type>* next;
    type value;
};

#endif /* NODE_H */ 


node.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
#include "node.h"

template <typename type>
node<type>::node()
        :previous(0),
        next(0),
        value(0)
{}
template <typename type>
node<type>::node(type p)
        :previous(0),
        next(0),
        value(p)
{}
template <typename type>
node<type>::node(const node<type>& p)
        :previous(0),
        next(0),
        value(p.value)
{}
template <typename type>
node<type>::~node() 
{
    previous = 0;
    next = 0;
}
template <typename type>
node<type>* node<type>::operator =(const node<type>* p)
{
    this->value = p->value;
    return this;
}
template <typename type>
node<type>* node<type>::operator =(const node<type>& p)
{
    this->value = p.value;
    return this;
}


nodeList.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
#ifndef NODELIST_H
#define	NODELIST_H

#include "node.h"

template <typename type>
class nodeList
{
public:
    nodeList();
    nodeList(const nodeList<type>& p);
    nodeList(node<type>& p, int i);
    ~nodeList();
    
    int length();
    bool isEmpty();
    node<type>* getNode(int p);
    node<type>* getFirstNode();
    node<type>* getLastNode();
    void insertNode(node<type>& p, int index);
    void insertFirstNode(node<type>& p);
    void insertLastNode(node<type>& p);
    void removeNode(int p);
    void removeFirstNode();
    void removeLastNode();
    
    nodeList<type>& operator = (const nodeList<type>& p);
    node<type>* operator [] (int p);
private:
    void destroy();
    node<type>* locateNode(int p);
    void copyNodeList(const nodeList<type>& p);
    node<type>* createNode(int value);
    node<type>* mFirstNode;
    node<type>* mLastNode;
    int mLength;
};

#endif /* NODELIST_H */ 


nodeList.cpp(most omitted because of length restrictions)
1
2
3
4
5
6
7
#include "nodeList.h"
template<typename type>
nodeList<type>::nodeList()
{
	//constructor stuff
}
/*omitted implementations, but understand it is there and (hopefully) correctly formatted*/
The code in nodeList.cpp needs to be inlined in nodeList.h.
Sorry, I'm still fairly new to c++. What do you mean by
The code in nodeList.cpp needs to be inlined in nodeList.h.
It means that a template class cannot be divided among different files. Both declaration and implementation are written inside the header file. Your template programming book/tutorial/etc. should have mentioned this. If it didn't, you should be looking for a replacement.
I guess it did, though very vaguely... Anyways thanks for the quick response!
Topic archived. No new replies allowed.