• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Template function declarations

I am attempting to create a template function as follows:

template <typename T> DWORD GetRegistryDword (T key)
     tstring subKey(key);


     return retVal;

The function works perfectly, but when I try to place it in a separate source file, I get a LNK1120, unresolved external.  I have created the files as follows:

template <typename T> DWORD GetRegistryDword (T key);

#include <windows.h>
#include <string>
#include "registry.h"
(see above implementation)


#include <windows.h>
#include <registry.h>

int main()
    DWORD dwReturn = GetRegistryDword("HKEY_LOCAL_MACHINE\\...\\blah");

I have noticed that when I move my implementation into registry.h I no longer get the unresolved external in main.cpp, but I don't really want to place the entire implementation for all of my template functions into header files.  Is there any way around this?
  • 2
1 Solution
Template functions have to be put in a header file if you which to use it from multiple source files.

You can not just put the declaration in the header, and then access the function.

You can only put the source code in the *.cpp file, if your compiler supports "export".
Most compilers do not support export.  This includes Visual C++ 6.0 & .Net

So move your entire template function to a header file that can be access by all source files (*.cpp) that which to use it.
dirtdartAuthor Commented:
Well, that really bites, but if that's the only way to work it, then I guess that's what I'll have to do.

Thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now