Problem with template class on GCC

S

Shivanand

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
 
K

kostas

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
 
S

Shivanand

Thanks Sumit
Thanks kostas,

Made necessary changes to the code. now it works.

Regards
Shivanand
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top