为什么这两个卡片对象不等同?

Why do these two Card objects not equate?

本文关键字:对象 两个 为什么      更新时间:2023-10-16

我正在实现一个Card对象,我正在努力理解为什么我的卡片相等性测试失败。以下是声明:

// card.h
namespace Game {
class Card {
public:
int rank; // 1 to 13
char suit;
Card(int r, char s);
~Card();
Card(const Card &other); // copy constructor
friend std::ostream& operator<<(std::ostream& out, const Card &c);
Card &operator=(const Card &c);
bool operator>(const Card &other);
bool operator<(const Card &other);
bool operator<=(const Card &other);
bool operator>=(const Card &other);
bool operator==(const Card &other);
bool operator!=(const Card &other);
};
}

和实施

//card.cpp
Game {
//...
bool Game::Card::operator==(const Card &other) {
return (this->rank == other.rank) && (this->suit == other.rank);
}
//...
}

在我的测试文件中,它使用googletest

// CardTests.cpp
#include "gtest/gtest.h"
#include <string>
#include "Card.h"
TEST(CardTests, EqualsOperator) {
Game::Card fourOfHearts1 = Game::Card(4, 'H');
Game::Card fourOfHearts2 = Game::Card(4, 'H');
ASSERT_TRUE(fourOfHearts1 == fourOfHearts2);
}

生成以下输出:

Value of: fourOfHearts1 == fourOfHearts2
Actual: false
Expected: true

为什么两个fourOfHearts变量不相等?

(this->suit == other.rank);中的错别字 我猜应该是(this->suit== other.suit);

相关文章: