Solved

template problem

Posted on 1998-09-03
7
226 Views
Last Modified: 2010-04-02
Hi All,

Why does the following code not function.
Microsoft Visual C++ 5.00

// Header File
class Math {
public:

// ... (Constructors destructors etc.... )
       template< class T > T Min( T a, T b );
       
       
};

// CPP File
template< class T > T Math::Min( T a, T b )
{
 return (a < b) ? a : b;
}

This will not link when the code above is entered.
The odd thing about this little caveat is that the following code
WILL work.

// (All in header file)
class Math {
public:

// ... (Constructors destructors etc.... )
       template< class T > T Min( T a, T b ) { return (a < b) ? a : b ; };
       
       
};

If anyone can convincingly explain why this is the case, the points are yours...
A workaround or something (perhaps namespaces are required???) would be appreciated.

Kind Regards
-craig and sean.
0
Comment
Question by:cmain
[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
  • 2
  • 2
7 Comments
 
LVL 8

Expert Comment

by:Answers2000
ID: 1172008
Change this

/// CPP File
     template< class T > T Math::Min( T a, T b )
     {
      return (a < b) ? a : b;
     }

to

// in header
inline  template< class T > T Math::Min( T a, T b )
     {
      return (a < b) ? a : b;
     }


Otherwise the template never gets expanded.

You're second version works because it's all in the header so can be expanded
0
 
LVL 1

Author Comment

by:cmain
ID: 1172009
Sorry, but I have been experimenting with this, and I have got nowhere.

If I put the entire implementation of the template function into the header file then it works.
if I try use the implementation in the CPP file, in the form

// CPP File
template<class TYPE>
TYPE Math::xMin( TYPE Param, TYPE Param2 )
{
      return (Param<Param2) ? Param : Param2 ;
}

i.e. if I use Math::Whatever
The function will not work.
I get errors ranging from linker problems to Min : function does not take two parameters.

Now, the reason I regected your answer is that this remains the case whether or not I use inline.

Examaple.
      template<class TYPE>
      inline TYPE xMin( TYPE Param, TYPE Param2 )
      { return (Param<Param2) ? Param : Param2 ; }
this will work.
      template<class TYPE>
      TYPE xMin( TYPE Param, TYPE Param2 )
      { return (Param<Param2) ? Param : Param2 ; }
and so will that.
(Both in the header file).

But move the implementation out and try to use
template<class TYPE>
TYPE Math::xMin( TYPE Param, TYPE Param2 )
{
      return (Param<Param2) ? Param : Param2 ;
}

in a CPP file, then the inline makes no difference, and you still get errors. (Why?)

if the function implementation is in the header file, then it works regardless of whether or not it is inline.

If you cannot put the template implementation into the CPP file, then I would like to know why. I want the implementation in the CPP file if possible.

Thanks
Sorry for the regected answer
Regards
-craig.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1172010
Answers2000 is correct.

In many ways, templates work like preprocessor macros. The compiler must see the actual code in order to instantiate it. That is, it happens at compiling time, not linking time.
0
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!

 
LVL 23

Expert Comment

by:chensu
ID: 1172011
I would suggest Answers2000 resubmit the answer.
0
 
LVL 1

Author Comment

by:cmain
ID: 1172012
Kewl,
You can only put the implementation in the header file because the templates work like preprocessor macros, and cannot be expanded if they are in a CPP file.

Answers2000,
Please resubmit your answer. Sorry about the inconvenience, but I needed to be sure.
Thanks very much for your assistance.

Regards
-craig.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1172013
OK, resubmitting,

For those for follow: see the answer from
"Thursday, September 03 1998 - 12:34PM PDT"

0
 
LVL 1

Author Comment

by:cmain
ID: 1172014
I have accepted your answer, but would like to know how the STL gets around this problem, as a lot of their implementations are in CPP files.

I have realised that my understanding of templates is a bit rusty, so I am going back to the drawing board on this one anyway.

Thanks for your help.
Regards
-craig.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 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