Sign in to follow this  

C++ templated classes and LNK2019

This topic is 4669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know what usually causes an LNK2019, and I thought that I had accomodated for it. I'm most likely missing something fundamental about templates in a class, but I can't for the life of me figure out what I've done wrong here. Here is my problem in a reduced form, split into three source files. //-- main.cpp:
[SOURCE]#include "My_Class.h"

int main()
{
	My_Class<int> object1;
	return 1;
}[/SOURCE]
//-- My_Class.h
[SOURCE]#pragma once
template <class T>
class My_Class
{
public:
	My_Class();
	void myFunction(T * tPtr);
};[/SOURCE]
//-- My_Class.cpp
[SOURCE]#include "My_Class.h"

template <class T>
My_Class<T>::My_Class()
{
	return;
}

template <class T>
void My_Class<T>::myFunction(T * tPtr)
{
	return;
}
[/SOURCE]
I get a LNK2049, saying: LNK2019: unresolved external symbol "public: __thiscall My_Class<int>::My_Class<int>(void)" (??0?$My_Class@H@@QAE@XZ) referenced in function _main It compiles fine, needless to say. It's like it doesn't ever implement the case where T == int, but I can't figure out why. Documentation didn't help either. I'm using Microsoft Visual Studio .NET 2003 Any help or hints would be appreciated.

Share this post


Link to post
Share on other sites
Template code must be defined in the same translation unit they where declared in, unless a compiler supports the "export" keyword and VC++ 7.1 doesn't as is the case with most compilers.

Share this post


Link to post
Share on other sites

This topic is 4669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this