Connecting Tech Pros Worldwide Forums | Help | Site Map

Instantiation of template method in template class

Ed
Guest
 
Posts: n/a
#1: Aug 27 '08
Hi, guys,
I declare a template method in one template class in one library.
Compiling is OK, but link is not OK.

Header File is:
<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.

The link error is:
2>Linking...
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
Guest
 
Posts: n/a
#2: Aug 27 '08

re: Instantiation of template method in template class


On Aug 27, 2:35*pm, Ed <seah...@gmail.comwrote:
Quote:
Hi, guys,
I declare a template method in one template class in one library.
Compiling is OK, but link is not OK.
>
Header File is:
<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.
>
The link error is:
2>Linking...
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);


Closed Thread