Re: Linking issue while using C++ Templates

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

 



Thanks a lot for the help Ian. It solves the problem.

However I was not sure, if it has nothing to do with gcc as such, then why was it working fine with gcc egcs-2.91.66. Any idea ?

 Thanks,
Rajat.



----- Original Message ----
From: Ian Lance Taylor <iant@xxxxxxxxxx>
To: Rajat <myself_rajat@xxxxxxxxx>
Cc: gcc-help@xxxxxxxxxxx
Sent: Wednesday, September 23, 2009 11:18:02 AM
Subject: Re: Linking issue while using C++ Templates

Rajat <myself_rajat@xxxxxxxxx> writes:

> //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;
> };

...

> 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 ?

You have to define your static variables, not just declare them.  This
is standard C++--it doesn't have anything to do with gcc as such.

template<typename T> A<T>* A<T>::_instance;

Ian



      

[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