C ++ prüfe, ob zwei Objekte mit dem Operator overloading == gleich sind, immer false?

Ich versuche zu überprüfen, ob zwei Objekte gleich sind, indem ich den Operator '==' für die Klasse überlade. Basierend auf allem, was ich hier über Stack Overflow und anderswo gelesen habe, zum Beispiel diesen Beitrag:

C ++ Objektgleichheit

Ich hätte gedacht, dass ich das richtig mache, aber ich muss etwas vermissen, da mein Vergleich immer falsch ist, auch wenn die Objekte gleich sind. Hier ist die Funktion, in der sie verglichen werden, die erste if-Anweisung:

///////////////////////////////////////////////////////////////////////////////////////////////////
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);
}

Hier ist die == Überschreibung in PossibleChar.h:

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

Hier ist alles von PossibleChar.h, wenn sich jemand fragt:

// 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

Eine Idee, was ich vermisse? Jede Hilfe wäre dankbar.

Antworten auf die Frage(4)

Ihre Antwort auf die Frage