?
Solved

Function templates and namespaces

Posted on 2003-12-04
3
Medium Priority
?
319 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
[X]
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
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 400 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

Independent Software Vendors: 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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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