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_LOC AL_MACHINE \\...\\bla h");
}
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?
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_LOC
}
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Thanks for the help.
You can not just put the declaration in the header, and then access the function.
Continue....