Gurmail Gurmail - 3 months ago 6
C++ Question

c++ create, assign and compare a new variable to two object inside an Operator Overloaded function

Apologize for my english. I'm trying to solve a very simple problem in c++. But i'm new in c++ so can't solve this :(
I'm trying to use operator overloading with header files. For new I'm trying to implementing this question. But I got error in getStatusPoint method.
enter image description here

int getStatusPoint(Alien const& alien)
{
return alien.getHeight()*alien.getWeight()*aliean.getGender();
}

bool Alien::operator>(const Alien& alien) const {
return(getStatusPoint(*this) > getStatusPoint(alien));
}


If I use don't use getStatusPoint method then its working fine.
Can anyone please help me to solve this question using getStatusPoint().

Answer

Your error message suggests that neither getHeight nor getWeight is marked as const. You should define

class Alien ... {
public:
  int getHeight() const { ... }
  ...
};

Your implementation should neither write access to a field of your class nor call a non-const method of your class.

Comments