Instantiation of template method in template class

Discussion in 'C++' started by Ed, Aug 27, 2008.

  1. Ed

    Ed Guest

    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.
     
    Ed, Aug 27, 2008
    #1
    1. Advertising

  2. Ed

    Ed Guest

    On Aug 27, 2:35 pm, Ed <> 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>::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);
     
    Ed, Aug 27, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robc
    Replies:
    1
    Views:
    398
    Jakob Bieling
    Aug 8, 2003
  2. christopher diggins
    Replies:
    16
    Views:
    769
    Pete Becker
    May 4, 2005
  3. Replies:
    1
    Views:
    587
    Salt_Peter
    Dec 25, 2006
  4. Noah Roberts
    Replies:
    6
    Views:
    1,198
    Johannes Schaub (litb)
    Feb 2, 2011
  5. Dave Mihalik
    Replies:
    6
    Views:
    161
    Dave Mihalik
    Jul 28, 2006
Loading...

Share This Page