Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Instantiation of template method in template class (http://www.velocityreviews.com/forums/t632543-instantiation-of-template-method-in-template-class.html)

 Ed 08-27-2008 06:35 AM

Instantiation of template method in template class

Hi, guys,
I declare a template method in one template class in one library.
Compiling is OK, but link is not OK.

<code>
template <typename P = float>
class TESTLIB_API Linear
{
public:
P x;
P y;
P z;

Linear(P x_, P y_, P z_): x(x_), y(y_), z(z_)
{
}

template <typename R>
void Distance(Linear<R>& rlinear);
};
</code>

Source CPP file is:
<code>
template <typename P>
template <typename R>
void Linear<P>::Distance(Linear<R>& rlinear)
{
x = (P) rlinear.x;
}

template class Linear<>;
template class Linear<double>;

template void Linear<float>::Distance(Linear<float>& rlinear);
template void Linear<float>::Distance(Linear<double>& rlinear);
template void Linear<double>::Distance(Linear<float>& rlinear);
template void Linear<double>::Distance(Linear<double>& rlinear);
</code>

Main.cpp is:
<code>
Linear<> la(1,1,1);
Linear<> lb(2,2,2);

la.Distance(lb);
</code>

Template and its implementation are put in library, and main code is
in application.

2>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/PROFILE'
specification
2>Ball.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/
OPT:REF' specification
2>Hello.obj : error LNK2019: unresolved external symbol "public: void
__thiscall Linear<float>::Distance<float>(class Linear<float> &)" (??
\$Distance@M@?\$Linear@M@@QAEXAAV0@@Z) referenced in function _main
2>D:\My_Documents\Project\C++\Hello\Debug\Hello.ex e : fatal error
LNK1120: 1 unresolved externals

Actually, I explicit declare the function in source cpp file. I don't
know what's the next I can do.

Thanks,
Ed.

 Ed 08-27-2008 06:41 AM

Re: Instantiation of template method in template class

On Aug 27, 2:35*pm, Ed <seah...@gmail.com> wrote:
> Hi, guys,
> I declare a template method in one template class in one library.
> Compiling is OK, but link is not OK.
>
> <code>
> template <typename P = float>
> class TESTLIB_API Linear
> {
> public:
> * * P x;
> * * P y;
> * * P z;
>
> * * Linear(P x_, P y_, P z_): x(x_), y(y_), z(z_)
> * * {
> * * }
>
> * * template <typename R>
> * * void Distance(Linear<R>& rlinear);};
>
> </code>
>
> Source CPP file is:
> <code>
> template <typename P>
> template <typename R>
> void Linear<P>::Distance(Linear<R>& rlinear)
> {
> * * x = (P) rlinear.x;
>
> }
>
> template class Linear<>;
> template class Linear<double>;
>
> template void Linear<float>::Distance(Linear<float>& rlinear);
> template void Linear<float>::Distance(Linear<double>& rlinear);
> template void Linear<double>::Distance(Linear<float>& rlinear);
> template void Linear<double>::Distance(Linear<double>& rlinear);
> </code>
>
> Main.cpp is:
> <code>
> * * Linear<> la(1,1,1);
> * * Linear<> lb(2,2,2);
>
> * * la.Distance(lb);
> </code>
>
> Template and its implementation are put in library, and main code is
> in application.
>
> 2>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/PROFILE'
> specification
> 2>Ball.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/
> OPT:REF' specification
> 2>Hello.obj : error LNK2019: unresolved external symbol "public: void
> __thiscall Linear<float>::Distance<float>(class Linear<float> &)" (??
> \$Distance@M@?\$Linear@M@@QAEXAAV0@@Z) referenced in function _main
> 2>D:\My_Documents\Project\C++\Hello\Debug\Hello.ex e : fatal error
> LNK1120: 1 unresolved externals
>
> Actually, I explicit declare the function in source cpp file. I don't
> know what's the next I can do.
>
> Thanks,
> Ed.

Ahh... I know it.
I need to put a "dllexport" before the instantiation of template class
and template method, because they are declared in the library.

Corrected code is the following:

Source CPP file is:

template <typename P>
template <typename R>
void Linear<P>::Distance(Linear<R>& rlinear)
{
x = (P) rlinear.x;
}

template TESTLIB_API class Linear<>;
template TESTLIB_API class Linear<double>;

template TESTLIB_API void Linear<float>::Distance(Linear<float>&
rlinear);
template TESTLIB_API void Linear<float>::Distance(Linear<double>&
rlinear);
template TESTLIB_API void Linear<double>::Distance(Linear<float>&
rlinear);
template TESTLIB_API void Linear<double>::Distance(Linear<double>&
rlinear);

 All times are GMT. The time now is 03:31 PM.