template<> compiler errors

C

Coltrane

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
 
A

Andrey Tarasevich

Coltrane said:
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 */
 
C

Coltrane

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,117
Latest member
Matilda564
Top