I have a really simple problem (I think), but I can't work out for the life of me what it is. I have 3 files:
Code:
// link.h
#ifndef LINK_H
#define LINK_H
template<class T> class Link
{
private:
T data;
Link<T> *nextLink;
public:
Link(T d);
T get();
void update(T newData);
};
#endif
Code:
// link.cpp
#include "link.h"
template<class T> Link<T>::Link(T d)
{
data = d;
}
template<class T> T Link<T>::get()
{
return data;
}
template<class T> void Link<T>::update(T newData)
{
data = newData;
}
Code:
// linktest.cpp
#include "link.h"
int main()
{
Link<int> myLink(4);
}
I'm trying to compile this with "g++ linktest.cpp link.cpp -o linktest" and am getting undefined reference errors:
/tmp/ccUiS6RY.o(.text+0x26): In function `main':
/home/nick/C++/linktest.cpp:7: undefined reference to `Link<int>::Link(int)'
collect2: ld returned 1 exit status
If I move the definition of the constructor into link.h, i.e.
Code:
// link.h
...
public:
Link(T d) { data = d; }
...
then it compiles and links with no errors.
What am I doing wrong when I'm trying to use separate files?
Thanks!