Solved

Template function declarations

Posted on 2002-07-26
3
530 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

696 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