Solved

Function templates and namespaces

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

Industry Leaders: 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

696 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