Solved

Template function declarations

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

930 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

12 Experts available now in Live!

Get 1:1 Help Now