[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

template<> compiler errors

john coltrane

12/17/2008 9:52:00 PM

I am trying to compile some code from the "C++ Programming Language
Special Edition" book and I am getting compiler errors with Visual C+
+
and g++. The sample code is in appendix C, section C.13.

The code is as follows:


template<class T>
class X{
static T def_val;
static T* new_X(T a = def_val);



};


template<class T> T X<T>::def_val(0,0);
template<class T> T* X<T>::new_X(T a) {}

template<> int X<int>::def_val<int> = 0; /*line 10 */
template<> int* X<int>::new_X<int>(int i){} /* line 11 */


I get the following errors with Visual C++


Error error C2143: syntax error : missing ';' before
'<' line 10
Error error C2988: unrecognizable template declaration/
definition line 10
Error error C2059: syntax error :
'<' line 10
Error error C2143: syntax error : missing ';' before
'<' line 11
Error error C2470: 'X<T>::new_X' : looks like a function
definition,
but there is no parameter list; skipping apparent body line 11
Error error C2988: unrecognizable template declaration/
definition line 11
Error error C2059: syntax error :
'<' line 11


When I compile this with g++ I get:


templates.cpp:10: error: expected initializer before '<' token
templates.cpp:11: error: expected initializer before '<' token


can someone tell me what the problem is.
Is this a problem with the compiler or am I just messed up in the
head
(very possible)


thanks for the help


john


2 Answers

Andrey Tarasevich

12/17/2008 9:59:00 PM

0

Coltrane wrote:
> I am trying to compile some code from the "C++ Programming Language
> Special Edition" book and I am getting compiler errors with Visual C+
> +
> and g++. The sample code is in appendix C, section C.13.
>
> The code is as follows:
>
>
> template<class T>
> class X{
> static T def_val;
> static T* new_X(T a = def_val);
>
>
>
> };
>
>
> template<class T> T X<T>::def_val(0,0);
> template<class T> T* X<T>::new_X(T a) {}
>
> template<> int X<int>::def_val<int> = 0; /*line 10 */
> template<> int* X<int>::new_X<int>(int i){} /* line 11 */
> ...
> can someone tell me what the problem is.
> Is this a problem with the compiler or am I just messed up in the
> head
> (very possible)

It is not supposed to have those second <int>'s in the specialized
definitions

template<> int X<int>::def_val = 0; /*line 10 */
template<> int* X<int>::new_X(int i) {} /* line 11 */

--
Best regards,
Andrey Tarasevich

john coltrane

12/17/2008 10:52:00 PM

0

On Dec 17, 4:59 pm, Andrey Tarasevich <andreytarasev...@hotmail.com>
wrote:
> Coltrane wrote:
> > I am trying to compile some code from the "C++ Programming Language
> > Special Edition" book and I am getting compiler errors with Visual C+
> > +
> > and g++. The sample code is in appendix C, section C.13.
>
> > The code is as follows:
>
> > template<class T>
> > class X{
> >    static T def_val;
> >    static T* new_X(T a = def_val);
>
> > };
>
> > template<class T> T X<T>::def_val(0,0);
> > template<class T> T* X<T>::new_X(T a) {}
>
> > template<> int X<int>::def_val<int> = 0;        /*line 10 */
> > template<> int* X<int>::new_X<int>(int i){}    /* line 11 */
> > ...
> > can someone tell me what the problem is.
> > Is this a problem with the compiler or am I just messed up in the
> > head
> > (very possible)
>
> It is not supposed to have those second <int>'s in the specialized
> definitions
>
>    template<> int X<int>::def_val = 0;        /*line 10 */
>    template<> int* X<int>::new_X(int i) {}    /* line 11 */
>
> --
> Best regards,
> Andrey Tarasevich- Hide quoted text -
>
> - Show quoted text -

thanks