Solved

Function templates and namespaces

Posted on 2003-12-04
3
312 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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now