I'm trying to overload the comparison operator for a stock class I have created, but whenever I try to compile it gives me an error that says "request for members of non-aggregate type before '(' token" and "passing `const stock' as `this' argument of `int stock::getDate()' discards qualifiers"
bool stock::operator==(const stock &other)
{
if(*this.getDate() == &other.getDate() && *this.getTicker() == &other.getTicker())
returntrue;
returnfalse;
}
bool stock::operator<(const stock &other)
{
// check if the date is less
if(*this.getDate() < &other.getDate())
returntrue;
// if the date is the same check the ticker
elseif(*this.getDate() == &other.getDate())
if(*this.getTicker() < &other.getTicker())
returntrue;
returnfalse;
}
First, you don't need to explicitly call this at all, if you don't want to, you can just call getDate() and it implicitly uses this.
Second, the problem is that in your operators your parameters are const stock objects, which means that only const member functions can be used on those objects, notice your getDate() member is not a const member function (but it should be), in fact I would make all accessors that don't change the members of the object const, including your operators (== and <).
thanks for your help. I finally got it to work by using date == other.date etc... It seems weird that I had to do it that way, but the accesser functions wouldn't compile.