I have a question for a specific friendship of templates in C ++. in the C ++ Primer book, concrete template friendships are written as follows:
template <class T> class Foo3; template <class T> void templ_fcn3(const T&); template <class Type> class Bar {
The special point is that there is
<Type>
after the class or function name in a friend statement.
However, in practice, if I write this:
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... }
An error will be made for the template function during instances.
And if I change
// Specific Template Friendship friend T_CheckPointer<Type> operator+ <Type> (const T_CheckPointer<Type> &, const size_t n);
to
// Specific Template Friendship friend T_CheckPointer<Type> operator+ <> (const T_CheckPointer<Type> &, const size_t n);
deleting the type word after the function name, then everything will be fine.
Can anyone tell me the reason?
For information, a message appears. when i call
int iarr[] = {1, 2, 3, 4}; T_CheckPointer<int> itcp(iarr, iarr+4);
error message:
/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