检查Array中元素的相等性 - c ++

use*_*253 2 c++ arrays equals

为了检查一个简单数组中的相等性,我有以下几点;

int a[4] = {9,10,11,20};
    if(a[3]== 20){
        cout <<"yes"<< endl;
    }
Run Code Online (Sandbox Code Playgroud)

但是,当我创建一个类型类的数组,并尝试检查相等性时,我得到错误;

Human是一个具有名称,年龄,性别等私有变量的类,并为这些变量获取和设置函数.

humanArray的大小为20

void Animal::allocate(Human h){
    for (int i =0; i<20; i++){
        if(humanArray[i] == h){
            for(int j = i; j<size; j++){
                humanArray[j] = humanArray[j +1];
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到以下错误;

error: no match for 'operator==' in '((Animal*)this)->Animal::humanArray[i] == h'|
Run Code Online (Sandbox Code Playgroud)

我可以传入索引和Human,并检查索引号.但是,有没有办法检查两个元素是否相同?我不想检查说出人类名字的"人名",因为对于某些部分,我的人类没有名字.

tem*_*def 6

为了制作语法

if(humanArray[i] == h)
Run Code Online (Sandbox Code Playgroud)

合法的,你需要operator==为你的人类定义.为此,您可以编写一个如下所示的函数:

bool operator== (const Human& lhs, const Human& rhs) {
   /* ... */
}
Run Code Online (Sandbox Code Playgroud)

在这个函数中,你会做的场逐场比较lhs,并rhs看看他们都是平等的.从现在开始,只要您尝试使用==运算符比较任何两个人,C++就会自动调用此函数进行比较.

希望这可以帮助!