Solved

template problem

Posted on 1998-09-03
7
206 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

760 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

21 Experts available now in Live!

Get 1:1 Help Now