Solved

Function templates and namespaces

Posted on 2003-12-04
3
314 Views
Last Modified: 2010-04-01
Ok, I have 3 files: compare.h, compare.cpp, and main.cpp
I am trying to write a function template to compare two numbers and return the largest. I want to put the template prototype in compare.h, template definition in compare.cpp, and the code in main.cpp
My book explains how to do it with functions, but not with function templates. K, here is the code as I wrote it.

compare.h
=======

namespace compare
{
    template<class T> T max(const T num1, const T num2);
}

compare.cpp
========

#include "compare.h"

template<class T> T compare::max(const T num1, const T num2)
{
    return (num1 > num2 ? num1 : num2);
}


main.cpp
======

#include <iostream>
#include "compare.h"

using compare::max;

int main()
{
    std::cout << max(5.0, 7.0);

    return 0;
}



==========================
I recieve "unresolved externals .." when trying to compile. Also, in by book, it says "Because we are using class templates you will need to prefix each of the template definitions with keyword 'export'.  The code will the be fully compatible with ANSI/ISO standard .."
Question is, where exactly do I put this 'export' keyword?

Thanks.
0
Comment
Question by:krupini
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 100 total points
ID: 9879441
Per the standard, you should implement the function as....

export template<class T> T compare::max(const T num1, const T num2)
{
    return (num1 > num2 ? num1 : num2);
}

...in compare.cpp.

However, I recommend that you put...

template<class T> T compare::max(const T num1, const T num2)
{
    return (num1 > num2 ? num1 : num2);
}

...into your header and lose the compare.cpp file, because not many compilers support the export keyword.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9879588
@krupini:

>  I want to put the template prototype in compare.h, template definition in
> compare.cpp, and the code in main.cpp

For templates, you don't put the code in a .CPP file.  The linker has trouble finding it that way.  Just put the prototype and the main code in the header file (or don't have a prototype, just stick it in your header and be done with it!).

Hope That Helps,
Dex*
0
 

Author Comment

by:krupini
ID: 9879605
Ah, very true. I have Visual Studio 2003 and Dev-Cpp (using GCC 3.2) and none of them support the 'export' keyword. Thanks!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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