Problem with template class on GCC

Discussion in 'C++' started by Shivanand, May 10, 2007.

  1. Shivanand

    Shivanand Guest

    Hello everyone,

    Im facing problems with linking C++ templates in GCC on a MAC.

    Following is the error GCC throws when I put template declaration in
    ".h" file and definition in ".cpp" file.
    /usr/bin/ld: Undefined symbols:
    JQualifier<double, 3>::JQualifier(int, long)
    JQualifier<double, 3>::~JQualifier()

    I get no errors when I put the class definitions inline (i.e. define
    the instance methods inside the class). Can anyone please tell me what
    I am doing wrong ?

    Thanks in Advance

    Following is the code snippet :

    //--------------- JQualifier.h ---------------------------
    template <typename T, int n>
    class JQualifier
    {
    public :
    JQualifier (int type, long uid);
    ~JQualifier () ;

    };

    //--------------- JQualifier.h ---------------------------


    //--------------- JQualifier.cpp ---------------------------
    #include "JQualifier.h"

    template <typename T, int n>
    JQualifier<T, n>::JQualifier (int type, long uid)
    {

    }

    template <typename T, int n>
    JQualifier<T, n>::~JQualifier ()
    {

    }
    //--------------- JQualifier.cpp ---------------------------

    //---------------- Main.cpp ----------------------------------

    #include <iostream>
    #include <JQualifier.h>

    int main (int argc, char * const argv[]) {

    JQualifier<double, 3> obj (1, 1) ;
    return 0;
    }

    //---------------- Main.cpp ----------------------------------

    Regards
    Shivanand
    Shivanand, May 10, 2007
    #1
    1. Advertising

  2. Shivanand

    kostas Guest

    On May 10, 9:26 am, Shivanand <> wrote:
    > Hello everyone,
    >
    > Im facing problems with linking C++ templates in GCC on a MAC.
    >
    > Following is the error GCC throws when I put template declaration in
    > ".h" file and definition in ".cpp" file.
    > /usr/bin/ld: Undefined symbols:
    > JQualifier<double, 3>::JQualifier(int, long)
    > JQualifier<double, 3>::~JQualifier()
    >
    > I get no errors when I put the class definitions inline (i.e. define
    > the instance methods inside the class). Can anyone please tell me what
    > I am doing wrong ?


    it's a faq
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.13
    kostas, May 10, 2007
    #2
    1. Advertising

  3. Shivanand

    Sumit Rajan Guest

    kostas wrote:
    > On May 10, 9:26 am, Shivanand <> wrote:
    >> Hello everyone,
    >>
    >> Im facing problems with linking C++ templates in GCC on a MAC.
    >>
    >> Following is the error GCC throws when I put template declaration in
    >> ".h" file and definition in ".cpp" file.
    >> /usr/bin/ld: Undefined symbols:
    >> JQualifier<double, 3>::JQualifier(int, long)
    >> JQualifier<double, 3>::~JQualifier()
    >>
    >> I get no errors when I put the class definitions inline (i.e. define
    >> the instance methods inside the class). Can anyone please tell me what
    >> I am doing wrong ?

    >
    > it's a faq
    > http://www.parashift.com/c -faq-lite/templates.html#faq-35.13
    >


    See:
    http://www.parashift.com/c -faq-lite/templates.html#faq-35.12

    You may like to read all the Q&A's in that section.


    --
    Sumit Rajan <>
    Sumit Rajan, May 10, 2007
    #3
  4. Shivanand

    Sumit Rajan Guest

    Sumit Rajan, May 10, 2007
    #4
  5. Shivanand

    Shivanand Guest

    Thanks Sumit
    Thanks kostas,

    Made necessary changes to the code. now it works.

    Regards
    Shivanand
    Shivanand, May 10, 2007
    #5
    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. Walt Karas
    Replies:
    13
    Views:
    756
    Sharad Kala
    Aug 31, 2004
  2. christopher diggins
    Replies:
    16
    Views:
    736
    Pete Becker
    May 4, 2005
  3. Replies:
    5
    Views:
    353
    Nathan Addy
    Sep 17, 2005
  4. Replies:
    2
    Views:
    757
  5. A L
    Replies:
    1
    Views:
    505
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page