Template classes broken into seperate files?

okay im having a bit of a problem with this...
this wont work..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//MushNode.h
#ifndef _MUSHNODE_H
#define _MUSHNODE_H
namespace MushBase{
    template <class T>
    class MushNode{
        public:
        short type;
        T data;
        MushNode* prev;
        MushNode* next;
        //
        MushNode(T dat);
    };
}
#endif//_MUSHNODE_H 

1
2
3
4
5
6
7
8
//MushNode.cpp
#include "MushNode.h"
namespace MushBase{
    template <class T>
    MushNode<T>::MushNode(T dat){
        data = dat;
    }
}

1
2
3
4
5
//main
#include "MushNode.h"
int main(){
    MushBase::MushNode<int> var(6);
}

but this will work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//main
namespace MushBase{
    template <class T>
    class MushNode{
        public:
        short type;
        T data;
        MushNode* prev;
        MushNode* next;
        //
        MushNode(T dat);
    };
    template <class T>
    MushNode<T>::MushNode(T dat){
        data = dat;
    }
}
int main(){
    MushBase::MushNode<int> var(6);
}


this one is blowing my mind, the second version is copy past from the first, but it works and the other doesnt, only difference is that its in different files, i must be missing something but i cant find what it is...

here are the errors :
obj\Debug\main.o||In function `main':|
C:\Users\Mushroom\Documents\iunno\memoryBase\main.cpp|4|undefined reference to `MushBase::MushNode<int>::MushNode(int)'|
||=== Build finished: 1 errors, 0 warnings ===|
You need to use the export keyword to put the implementation of a template in a different file. Most compiler don't have this feature so you should put the implementation in the header file
http://www.cplusplus.com/forum/articles/14272/
http://www.cplusplus.com/forum/articles/10627/ - section 8 -
Last edited on
thank you for your help! i will read both of those and see if i can fix my problem!
Topic archived. No new replies allowed.