I’m working on an container class template (for int,bool,strings etc), and I’ve been stuck with this error
cont.h:56: error: expected initializer before '&' token
for this section
template <typename T>
const Container & Container<T>::operator=(const Container<T> & rightCont){
what exactly have I done wrong there?.
Also not sure what this warning message means.
cont.h:13: warning: friend declaration `bool operator==(const Container<T>&, const Container<T>&)' declares a non-template function
cont.h:13: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
at this position
template <typename T>
class Container{
friend bool operator==(const Container<T> &rhs,const Container<T> &lhs);
public:
In the first case you’ve done things backwards. When you specify the return type, you have to include the template parameter list into the template identifier (
Container<T>), but when you specify parameter type, you don’t need to do it (justContaineris enough)You did it the other way around for some reason.
In the second case, when you declare
operator ==as a friend it simply warns you that that in this caseoperator ==you are referring to is an ordinary function. It can’t be a specialization of a template. I.e. for the classContainer<int>the functionwill be a friend. But specialization of function template
for
U == intwill not be a friend ofContainer<int>. If that’s your intent, you are OK.If you wanted to befriend a specific specialization of the above template, you’d have to say
If you wanted to befriend all specialization of the above template, you’d have to say