为什么这两个卡片对象不等同?
Why do these two Card objects not equate?
我正在实现一个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);
相关文章:
- 如何返回一个类的两个对象相加的结果
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 在什么情况下,两个堆栈分配的结构对象的 this 点指向同一个地址?
- 通过插槽和信号在不同线程中的两个qt对象之间进行通信
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何在Qt中连接来自不同窗口的两个对象?
- 是否可以使用非常量指针调用非常量函数,以及当两个unique_ptrs指向同一个对象时程序的行为方式?
- C++两个对象,其中包含指向同一数组不同部分的指针
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 绘制一个对象,比较模具缓冲区的两个不同值
- C++控制台应用,其中有两个冲突的对象不工作
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 当两个 std::map 对象相同时
- C++:在另外两个对象之间共享一个对象
- 为什么这两个卡片对象不等同?