Solved

template problem

Posted on 1998-09-03
7
209 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 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.

930 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

8 Experts available now in Live!

Get 1:1 Help Now