Linking issue while using C++ Templates

Rajat myself_rajat@yahoo.com
Wed Sep 23 04:47:00 GMT 2009


Hi All,

I am using C++ templates, and facing some strange linking issues with GCC 3.4.6. Here it goes,

I have a singalton class template, say A.h .
//A.h -- sample code.
template<typename T> class A
{
public:
    static A * getInstance ()
    {
        if (!_instance)
            _instance = new A ();
        return _instance;
    }

 T * getApp () const { return _pApp; }

private:
    T * _pApp;
    static A * _instance;
};


Another class B, as B.h,

//B.h
class B : public C
{
public:
//Some var and funcs are defined.
};


Now I am trying to use it in a .cpp file as,
//x.cpp
#include "A.h"
#include "B.h"
--------------------
--------------------
B * app = A<B>::getInstance ()->getApp ();
--------------------
--------------------


File x.cpp compiles perfactly, but while linking it throws error saying,
x.o(.gnu.linkonce.t._ZN9AI11BE11getInstanceEv+0x9): In function `A<B>::getInstance()':
A.h:: undefined reference to `A<B>::_instance'
collect2: ld returned 1 exit status

Any clue ?

Thanks,
Rajat.


      



More information about the Gcc-help mailing list