I'm trying to overload the assignment ('=') operator of a template class, but I need the operator=
method to be a friend function.
I though this would be simple, but I'm doing something wrong because the code below causes a compilation error:
error C2801: 'operator =' must be a non-static member
template <typename T>
class IDataStream
{
public:
friend void operator=(const IDataStream& dataStream)
{
// set some private members, e.g.
// this->{...} = dataStream.{...};
};
}
Can someone show me the error of my ways- I've become pretty stuck on this :( Thanks.
Your error is using
friend
, which changes the function from a member-function to aninline
-definedfriend
-function.operator=
can only be defined as a non-static member-function, and needs two arguments, the implicitthis
and the explicit right-hand-side.