|
From: jonas.ritter@gmx.de on 13 Dec 2005 13:33 I tried to compile the following code with g++ 3.4 and 4.0 and got the error: #include <iostream> template <class T> class X { static X<T> * xp; }; X<int> * X<int>::xp = NULL; int main() { return 0; } with g++ 3.2 it works was something changed in the compiler or in c++ standard? do I need to write the code differently? Thanks Jonas [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Tutone on 13 Dec 2005 18:02 jonas.ritter(a)gmx.de wrote: > I tried to compile the following code with g++ 3.4 and 4.0 and got the > error: > > #include <iostream> > template <class T> class X { > static X<T> * xp; > }; > > X<int> * X<int>::xp = NULL; change the above line to template<> X<int> * X<int>::xp = 0; (I'm not sure whether including <iostream> necessarily results in defining NULL.) > > int main() { > return 0; > } > > with g++ 3.2 it works > was something changed in the compiler or in c++ standard? The later versions of gcc are more conforming to the standard. > do I need to write the code differently? As shown above. Best regards, Tom [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Gene Bushuyev on 14 Dec 2005 01:13 <jonas.ritter(a)gmx.de> wrote in message news:1134474874.227483.125000(a)o13g2000cwo.googlegroups.com... >I tried to compile the following code with g++ 3.4 and 4.0 and got the > error: > > #include <iostream> > template <class T> class X { > static X<T> * xp; > }; > > X<int> * X<int>::xp = NULL; > > int main() { > return 0; > } > > with g++ 3.2 it works > was something changed in the compiler or in c++ standard? > do I need to write the code differently? > No, the standard didn't change, rather the compilers are getting better. In your code, you missed the template<> to define the explicit specialization: template<> X<int>* X<int>::xp = NULL; -- Gene Bushuyev ---------------------------------------------------------------- There is no greatness where there is no simplicity, goodness and truth. ~ Leo Tolstoy [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Maeder on 14 Dec 2005 01:11 "jonas.ritter(a)gmx.de" <jonas.ritter(a)gmx.de> writes: > I tried to compile the following code with g++ 3.4 and 4.0 and got the > error: > > #include <iostream> > template <class T> class X { > static X<T> * xp; > }; > > X<int> * X<int>::xp = NULL; Side note: NULL is not declared here. > int main() { > return 0; > } > > with g++ 3.2 it works > was something changed in the compiler or in c++ standard? The compiler was made more conforming. > do I need to write the code differently? template <> X<int> * X<int>::xp = NULL; [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: kanze on 14 Dec 2005 10:41
Thomas Tutone wrote: > jonas.ritter(a)gmx.de wrote: > > I tried to compile the following code with g++ 3.4 and 4.0 > > and got the error: > > #include <iostream> > > template <class T> class X { > > static X<T> * xp; > > }; > > X<int> * X<int>::xp = NULL; > change the above line to template<> X<int> * X<int>::xp = 0; > (I'm not sure whether including <iostream> necessarily results > in defining NULL.) It's undefined. In the case of g++, <iostream> does define NULL, so it's not his problem, but he should really add an #include <stddef.h> or #include <cstddef>, just to be sure. As other's have pointed out, his real problem is the absense of a template<>. Without that, his code doesn't compile, neither with NULL nor with 0. -- James Kanze GABI Software Conseils en informatique orient?e objet/ Beratung in objektorientierter Datenverarbeitung 9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |