Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Template function declarations

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

860 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