Tengo una pregunta para la amistad de plantilla específica en C++. en el libro de C++ cartilla, la amistad plantilla específica se escribe así:Plantilla específica Amistad en C++
template <class T> class Foo3;
template <class T> void templ_fcn3(const T&);
template <class Type> class Bar {
// each instantiation of Bar grants access to the
// version of Foo3 or templ_fcn3 instantiated with the same type
friend class Foo3<Type>;
friend void templ_fcn3<Type>(const Type&);
// ...
};
El punto especial es que hay
<Type>
después de que el nombre de la clase o función en el amigo comunicado.
Sin embargo, en la práctica, si escribo esto:
template <class Type> class T_CheckPointer;
template <class T> T_CheckPointer<T> operator+(const T_CheckPointer<T> &, const size_t n);
template <typename Type>
class T_CheckPointer {
// Specific Template Friendship
friend T_CheckPointer<Type>
operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);
// other code...
}
No será un error durante ejemplificaciones para la función de plantilla.
Y si cambio
// Specific Template Friendship
friend T_CheckPointer<Type>
operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);
a
// Specific Template Friendship
friend T_CheckPointer<Type>
operator+ <> (const T_CheckPointer<Type> &, const size_t n);
suprimiendo la palabra tipo después de que el nombre de la función, entonces todo estará bien.
¿Alguien puede decirme el motivo?
Para información, no es el mensaje error cuando llamo
int iarr[] = {1, 2, 3, 4};
T_CheckPointer<int> itcp(iarr, iarr+4);
mensaje de error:
/usr/include/c++/4.4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<int>’:
/usr/include/c++/4.4/bits/stl_iterator.h:96: instantiated from ‘std::reverse_iterator<int>’
../Classes/T_CheckPointer.hpp:31: instantiated from ‘T_CheckPointer<int>’
../PE16.cpp:520: instantiated from here
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:127: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:128: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:129: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:130: error: ‘int’ is not a class, struct, or union type
/usr/include/c++/4.4/bits/stl_iterator_base_types.h:131: error: ‘int’ is not a class, struct, or union type
¿Cuál es el mensaje de error? – enobayram
@enobayram, gracias por su atención, los he puesto en el artículo. – Tianyi
¿Podría proporcionar un ejemplo ** mínimo ** (que compila en, por ejemplo,Ideone) que muestra el problema? ¿Y trataste de actualizar a gcc 4.7? – TemplateRex