dolphin said:
Here is my situation:
I have a class named Date, is a class about date and time. I want
to use the cout<<Date.
You can't have a type in the expression like that. It has to be an
object of type 'Date', like
Date d;
cout << d;
> So I want to overload the operator. But why
this operator can not be a member function of this class?
[Assuming you mean that it can't be a non-static member..]
Of which class? Of the 'Date' class? It can. The Date object will
have to be on the left-hand side of the expression, however. You can do
it like this:
class Date {
...
void operator>>(std:
stream& os) const {
...
}
};
but then you will be writing
Date d;
d >> cout;
because in an expression 'a @ b' (where '@' is the operator), the
function that defines the operator will *either* be a two-argument
*non*-member, something like
returntype operator @(atype, btype);
or a *member* of 'atype' with a single explicit argument:
returntype atype:
perator &(btype);
What book on C++ are you reading that does not explain all that basic stuff?
V