Solved

Function templates and namespaces

Posted on 2003-12-04
3
315 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 62
Could someone please explain the following output from a linux based ldd -v  command? 6 139
Outlook 13 44
find Speed using GPS latitude & longitude 4 47
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

828 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