Ho una domanda per l'amicizia modello specifico in C++. nel libro C++ Primer, il modello specifico amicizia è scritto così:Amicizia modello specifico in 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&);
// ...
};
Il punto speciale è che non v'è
<Type>
dopo il nome della classe o funzione nel amico dichiarazione.
Tuttavia, in pratica, se scrivo questo:
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...
}
Ci sarà un errore durante istanze per la funzione di modello.
E se 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);
cancellando la parola tipo dopo il nome della funzione, allora tutto sarà a posto.
Chiunque può dirmi il motivo?
Per informazioni, c'è il messaggio di errore di quando chiamo
int iarr[] = {1, 2, 3, 4};
T_CheckPointer<int> itcp(iarr, iarr+4);
messaggio di errore:
/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
Qual è il messaggio di errore? – enobayram
@enobayram, grazie per l'attenzione, li ho messi nell'articolo. – Tianyi
Potresti fornire un ** esempio ** minimo (che viene compilato ad es.Ideone) che mostra il problema? E hai provato ad aggiornare a gcc 4.7? – TemplateRex