Solved

template problem

Posted on 1998-09-03
7
234 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
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!

 
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

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

623 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