Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Instantiation of template method in template class

Reply
Thread Tools

Instantiation of template method in template class

 
 
Ed
Guest
Posts: n/a
 
      08-27-2008
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>:istance(Linear<R>& rlinear)
{
x = (P) rlinear.x;
}

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

template void Linear<float>:istance(Linear<float>& rlinear);
template void Linear<float>:istance(Linear<double>& rlinear);
template void Linear<double>:istance(Linear<float>& rlinear);
template void Linear<double>:istance(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>:istance<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.
 
Reply With Quote
 
 
 
 
Ed
Guest
Posts: n/a
 
      08-27-2008
On Aug 27, 2:35*pm, Ed <(E-Mail Removed)> wrote:
> 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>:istance(Linear<R>& rlinear)
> {
> * * x = (P) rlinear.x;
>
> }
>
> template class Linear<>;
> template class Linear<double>;
>
> template void Linear<float>:istance(Linear<float>& rlinear);
> template void Linear<float>:istance(Linear<double>& rlinear);
> template void Linear<double>:istance(Linear<float>& rlinear);
> template void Linear<double>:istance(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>:istance<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>:istance(Linear<R>& rlinear)
{
x = (P) rlinear.x;
}


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

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of non-template member of template class Noah Roberts C++ 6 02-02-2011 09:53 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Prevent class instantiation from the initialize method? Dave Mihalik Ruby 6 07-27-2006 11:11 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
template method instantiation robc C++ 1 08-08-2003 12:25 PM



Advertisments