Jump to content
  • Advertisement
Sign in to follow this  
Blue Mind

C++ templated classes and LNK2019

This topic is 4972 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
Advertisement
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
I ran into the same problem a while ago and discovered that the definition of a template function has to be with the declaration of the function.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!