Skip to main content
gamedev.net
🔒 Locked

Link error when using templates

Started by JellyHead Nov 25, 2003 at 9:15 PM 0 replies 150+ views
Original Post
JellyHead
JellyHead
I get link errors when i try to use templates. Everything looks right.. well at least to me lists error LNK2019: unresolved external symbol "public: __thiscall someClass::someClass(void)" (??0?$someClass@H@@QAE@XZ) referenced in function _mainlists error LNK2019: unresolved external symbol "public: int __thiscall someClass:rint(void)" (?Print@?$someClass@H@@QAEHXZ) referenced in function _main
// main.cpp

#include "prac.h"

int main()
{
        someClass<int> aClass;
	aClass.Print();
}


// prac.h

template<class T>
class someClass
{
public:
	T someVar;
	int i;
	void Print();
	someClass();
	someClass(int j);
        ~someClass() {}
};

// prac.cpp

#include <iostream.h>
#include "prac.h"

template<class T>
someClass<T>::someClass()
{
	i = 0;
}

template<class T>
someClass<T>::someClass(int j)
{
	i = j;
}

template<class T>
void someClass<T>::Print()
{
    cout << i << endl;
}

xg0blin
xg0blin
Instead of naming your file a .cpp file, name it .template, and include it at the bottom of your header,

#include whatever.template

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.