C ++使用重载==运算符检查两个对象是否相等,始终为false? [英] C++ check if two objects are equal with overloading == operator, always false?

查看:68
本文介绍了C ++使用重载==运算符检查两个对象是否相等,始终为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过重载该类的'=='运算符来检查两个对象是否相等.基于我在此处有关堆栈溢出和其他地方阅读的所有内容,例如这篇文章:

I'm attempting to check if two objects are equal by overloading the '==' operator for the class. Based on everything I've read here on Stack Overflow and elsewhere, for example this post:

C ++对象平等

我本来可以正确地做到这一点,但我必须丢失一些东西,即使对象相同,我的比较也总是错误的.这是比较它们的函数,它是第一个if语句:

I would have figured I'm doing this correctly but I must be missing something b/c my comparison always comes up false, even when the objects are the same. Here is the function where they are compared, its the first if statement:

///////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<PossibleChar> findVectorOfMatchingChars(PossibleChar possibleChar, std::vector<PossibleChar> vectorOfChars) {
    std::vector<PossibleChar> vectorOfMatchingChars;                // this will be the return value

    for (auto possibleMatchingChar = vectorOfChars.begin(); possibleMatchingChar != vectorOfChars.end(); possibleMatchingChar++) {
        if (*possibleMatchingChar == possibleChar) {                // !!!!!!!!!!! this does not seem to work !!!!!!!!!!!!!!
            continue;                                               // !!!!!!!!!! it never gets in here, even when I'm 100% sure the variables refer to the same object
        }

        double dblDistanceBetweenChars = distanceBetweenChars(possibleChar, *possibleMatchingChar);
        double dblAngleBetweenChars = angleBetweenChars(possibleChar, *possibleMatchingChar);
        double dblChangeInArea = abs(possibleMatchingChar->intRectArea - possibleChar.intRectArea) / possibleChar.intRectArea;
        double dblChangeInWidth = abs(possibleMatchingChar->boundingRect.width - possibleChar.boundingRect.width) / possibleChar.boundingRect.width;
        double dblChangeInHeight = abs(possibleMatchingChar->boundingRect.height - possibleChar.boundingRect.height) / possibleChar.boundingRect.height;

        if (dblDistanceBetweenChars < (possibleChar.dblDiagonalSize * MAX_DIAG_SIZE_MULTIPLE_AWAY) &&
            dblAngleBetweenChars < MAX_ANGLE_BETWEEN_CHARS &&
            dblChangeInArea < MAX_CHANGE_IN_AREA &&
            dblChangeInWidth < MAX_CHANGE_IN_WIDTH &&
            dblChangeInHeight < MAX_CHANGE_IN_HEIGHT) {
            vectorOfMatchingChars.push_back(*possibleMatchingChar);
        }
    }

    return(vectorOfMatchingChars);
}

这里是============

Here is the == override in PossibleChar.h:

///////////////////////////////////////////////////////////////////////////////////////////////
bool operator == (const PossibleChar& otherPossibleChar) const {
    if (this == &otherPossibleChar) return true;
    else return false;
}

如果有人想知道,这里是所有可能的Char.h:

Here is all of PossibleChar.h if anybody is wondering:

// PossibleChar.h

#ifndef POSSIBLE_CHAR_H
#define POSSIBLE_CHAR_H

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

///////////////////////////////////////////////////////////////////////////////////////////////////
class PossibleChar {
public:
    // member variables ///////////////////////////////////////////////////////////////////////////
    std::vector<cv::Point> contour;

    cv::Rect boundingRect;

    int intCenterX;
    int intCenterY;

    double dblDiagonalSize;
    double dblAspectRatio;

    int intRectArea;

    ///////////////////////////////////////////////////////////////////////////////////////////////
    static bool sortCharsLeftToRight(const PossibleChar &pcLeft, const PossibleChar & pcRight) {
        return(pcLeft.intCenterX < pcRight.intCenterX);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////
    bool operator == (const PossibleChar& otherPossibleChar) const {
        if (this == &otherPossibleChar) return true;
        else return false;
    }

    // function prototypes ////////////////////////////////////////////////////////////////////////
    PossibleChar(std::vector<cv::Point> _contour);


};

#endif  // POSSIBLE_CHAR_H

知道我想念的是什么吗?任何帮助将不胜感激.

Any idea what I'm missing? Any help would be appreciated.

推荐答案

您当前的 operator == 假定对象位于相同的地址,因此如果您执行类似的操作

Your current operator== assumes the objects to be at the same address, so if you did something like

PossibleChar p1;
PossibleChar p2 = p1;
std::cout << p1 == p2 << '\n';

它将打印 false .

如果这确实是您想要的,那么.
如果不是不是,则需要定义一个 operator == ,它比较对象的所有必需 1 成员,而不是地址.

If that is indeed what you want, alright.
If it is not, you will need to define an operator==, which compares all necessary1 members of the objects, not the addresses.

1 这里的必需"是指定义对象状态的成员",即取决于要比较哪些成员的类.

1 "necessary" means "members defining the state of the object" here, that is, it depends on the class which members should be compared.

这篇关于C ++使用重载==运算符检查两个对象是否相等,始终为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆