Link to home
Start Free TrialLog in
Avatar of dirtdart
dirtdart

asked on

Template function declarations

I am attempting to create a template function as follows:

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

        //snip

     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:

registry.h:
template <typename T> DWORD GetRegistryDword (T key);

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

main.cpp:

#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?
Avatar of Axter
Axter
Flag of United States of America image

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.

Continue....
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dirtdart
dirtdart

ASKER

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.