Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

template problem

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
cmain
Asked:
cmain
  • 3
  • 2
  • 2
1 Solution
 
Answers2000Commented:
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
 
cmainAuthor Commented:
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
 
chensuCommented:
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!

 
chensuCommented:
I would suggest Answers2000 resubmit the answer.
0
 
cmainAuthor Commented:
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
 
Answers2000Commented:
OK, resubmitting,

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

0
 
cmainAuthor Commented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now