Solved

Template function declarations

Posted on 2002-07-26
3
500 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:dirtdart
  • 2
3 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7180356
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....
0
 
LVL 30

Accepted Solution

by:
Axter earned 200 total points
ID: 7180361
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.
0
 
LVL 5

Author Comment

by:dirtdart
ID: 7180370
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.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now