For the life of me, I can't figure out why this doesn't work. I've carefully double checked my work. Here's the problem:
I have a header file that looks something like this:
My source file looks something like this:
But trying to access the function I get an error message:
The error message I get (from VC++ 6.0, Service Pack 3) is:
If I define the function in the header file, either inline or externally in the same file, it works just fine.
What is my problem?
I have a header file that looks something like this:
Code:
// Example.h //
#include <iostream>
using namespace std;
#ifndef Example_h
#define Example_h
template <class Type> class Example {
protected:
Type t;
public:
Example (void) {}
Type getT (void);
void setT ( Type n ) { t = n; }
};
#endif
My source file looks something like this:
Code:
// Example.cpp //
#include "Example.h"
template<class Type>
Type Example<Type>::getT (void) { return t; }
Code:
// Driver.cpp //
#include <iostream>
using namespace std;
#include "Example.h"
void main (void)
{
Example<int> x;
cout << "Example x.t is " << x.getT() << ".\n";
}
Code:
error LNK2001: unresolved external symbol "public: int __thiscall Example<int>::getT(void)"
If I define the function in the header file, either inline or externally in the same file, it works just fine.
What is my problem?