Re: Template Functions
From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 01/20/05
- Next message: hans: "Re: Questions regarding waveIn* functions"
- Previous message: Charles: "Template Functions"
- In reply to: Charles: "Template Functions"
- Next in thread: Charles: "Re: Template Functions"
- Reply: Charles: "Re: Template Functions"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 20 Jan 2005 13:16:36 -0500
Charles wrote:
> [...] I just can't understand
> why the location of the code should matter. Any help would be greatly
> appreciated, PS. I am using Visual Studio .NET 2002.
Templates need to be fully visible to the compiler when it generates
the code to _instantiate_ them. The instantiation is either implicit
or explicit. When you don't have an explicit instantiation, the
compiler has to resort to implicit instantiation which it performs
when it sees the function (in your case) _used_, i.e. when compiling
the 'main.cpp' module. If the function template is not visible to
the compiler when compiling 'main.cpp', it is unable to instantiate
the template, i.e. create the code for that function. If it can't
create the code, the function doesn't exist (only the template for it
does), it can't find it during linking.
A way to solve it is to instantiate the function _explicitly_. That
is usually a good way to go about instantiating templates if you need
them in a library or something like that. Add to 'test.cpp':
-------- test.cpp
...
template void ConvertFToC<double>(double, double&);
--------
Read more about templates in "C++ Templates" by Vandevoorde and Josuttis.
V
- Next message: hans: "Re: Questions regarding waveIn* functions"
- Previous message: Charles: "Template Functions"
- In reply to: Charles: "Template Functions"
- Next in thread: Charles: "Re: Template Functions"
- Reply: Charles: "Re: Template Functions"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|