Intento aprender C++. En "The C++ Programming Language Third Edition" libro que encontré en la página de código 854 (Apéndice C.13.1):¿Es correcto el código en "La tercera edición del lenguaje de programación C++" en la página 854?
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;
template<> int* X<int>::new_X<int>(int i) { /* ... */ }
modifico que:
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) { return new T(a); }
template<> int X<int>::def_val<int> = 0;
template<> int* X<int>::new_X<int>(int i) { return new int(i); }
Pero mi compilador no compilar:
1>main.cpp(15): error C2143: syntax error : missing ';' before '<'
1>main.cpp(15): error C2988: unrecognizable template declaration/definition
1>main.cpp(15): error C2059: syntax error : '<'
1>main.cpp(16): error C2143: syntax error : missing ';' before '<'
1>main.cpp(16): error C2470: 'X<T>::new_X' : looks like a function definition, but there is no parameter list; skipping apparent body
1> with
1> [
1> T=int
1> ]
1>main.cpp(16): error C2988: unrecognizable template declaration/definition
1>main.cpp(16): error C2059: syntax error : '<'
1>main.cpp(19): error C2143: syntax error : missing ';' before '{'
1>main.cpp(19): error C2447: '{' : missing function header (old-style formal list?)
1>
1>Build FAILED.
¿Qué código incorrecto en el libro o compilador?
Sus mensajes de errror dan los números de línea, serían atento si se le da los números de línea en el código (o al menos escribir algo como "líneas x perdidos" en el primera línea de su código. Y donde viene el error, en el código original, o su? – flolo
modificado puede mostrar su main.cpp? – ysrb
La línea 15 es "plantilla <> int X :: def_val = 0;" –
ABEgorov