C ++ verifica se dois objetos são iguais ao operador sobrecarga ==, sempre falso?
Estou tentando verificar se dois objetos são iguais sobrecarregando o operador '==' da classe. Com base em tudo que li aqui no Stack Overflow e em outros lugares, por exemplo, este post:
Eu teria imaginado que estou fazendo isso corretamente, mas devo estar perdendo algo porque minha comparação sempre é falsa, mesmo quando os objetos são os mesmos. Aqui está a função em que eles são comparados, é a primeira instrução if:
///////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
Aqui está a substituição == em PossibleChar.h:
///////////////////////////////////////////////////////////////////////////////////////////////
bool operator == (const PossibleChar& otherPossibleChar) const {
if (this == &otherPossibleChar) return true;
else return false;
}
Aqui está todo o PossibleChar.h se alguém estiver se perguntando:
// 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
Alguma idéia do que estou perdendo? Qualquer ajuda seria apreciada.