Instantiation of template method in template class

E

Ed

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.exe : 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.
 
E

Ed

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.exe : 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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top