简体   繁体   中英

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. 在此处输入图片说明

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().

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.

int getHeight() const;一样声明您的getHeight()getWeight()getGender() const int getHeight() const;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM