Reputation: 3750
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:
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;
}
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.
Upvotes: 0
Views: 2383
Reputation: 15229
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';
it would print false
.
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 "necessary" means "members defining the state of the object" here, that is, it depends on the class which members should be compared.
Upvotes: 4
Reputation: 28241
Your operator==
returns true
only when comparing an object to itself. Two different objects will never be equal, because different objects have different addresses.
It seems that you really do want this behavior (though you don't specify it). However, your program copies objects around too much:
findVectorOfMatchingChars(PossibleChar possibleChar, ...);
The above declaration makes the function receive the "possible char" by value, copying it into a new object. This will never be equal to any other existing objects!
You probably want:
findVectorOfMatchingChars(const PossibleChar& possibleChar, ...);
This will pass the "possible char" by reference, and its address will be the address of an existing "possible char" (which, I guess, is one of the possible chars in your vector).
You might even pass an address:
findVectorOfMatchingChars(const PossibleChar* possibleChar, ...);
This will warn the unsuspecting user of your code that it does something with addresses. This will also prevent a naive user from passing a newly constructed object:
findVectorOfMatchingChars(PossibleChar(), ...); // error
PossibleChar x;
findVectorOfMatchingChars(&x, ...); // works
Upvotes: 1