Re: Linking issue while using C++ Templates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



More precisely, something like this,

$ g++ x.cpp
/tmp/ccA65pR9.o: In function `A<B>::getInstance(void)':
/tmp/ccA65pR9.o(.A<B>::gnu.linkonce.t.getInstance(void)+0x7): undefined reference to `A<B>::_instance'
/tmp/ccA65pR9.o(.A<B>::gnu.linkonce.t.getInstance(void)+0x36): undefined reference to `A<B>::_instance'
/tmp/ccA65pR9.o(.A<B>::gnu.linkonce.t.getInstance(void)+0x3c): undefined reference to `A<B>::_instance'
collect2: ld returned 1 exit status


Thanks,
Rajat.



----- Original Message ----
From: Rajat <myself_rajat@xxxxxxxxx>
To: gcc-help@xxxxxxxxxxx
Sent: Wednesday, September 23, 2009 10:17:07 AM
Subject: Linking issue while using C++ Templates

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.


      

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux