CAN A TEMPLATE CLASS HAVE A FREIND FUNCTION IN IT?
ayanda83 (97)
Oct 29, 2012 at 3:56pm UTC
Hi everyone, Can a template class have a friend function? like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
template <class Type>
class myClass
{
friend void foo();
public :
...
};
template <class Type>
void myClass<Type>::foo()
{
...
}
I'm asking this because I have a program i'm working on which is of a similar nature and it keeps on giving me the following error: 21:90 C:\UNISA MODULES\COS2611\MY PROJECTS\Assignment_2Q1a.cpp [Warning] friend declaration 'void deleteOc(const unoderedLinkedList<Type>&, unoderedLinkedList<Type>&)' declares a non-template function [-Wnon-template-friend]
[Linker Error] undefined reference to `deleteOc(unoderedLinkedList<char> const&, unoderedLinkedList<char>&)'
codewalker (159)
Oct 29, 2012 at 4:02pm UTC
Yes they can. The warning is generated mainly because you have defined that function as member of the class rather than a global function.
vlad from moscow (3112)
Oct 29, 2012 at 5:33pm UTC
You declared non-template friend function but are trying to define it as a template function.
ayanda83 (97)
Oct 29, 2012 at 11:45pm UTC
@codewalker, But why am I being warned, if what I did is right. @vlad I'd assume that, because the function is a friend of a template class, the function should also be defined as a template function. I may be wrong, if I am, please tell me how.
Topic archived. No new replies allowed.