C2678 二进制 '==':未找到采用 'Card' 类型左操作数的运算符(或者没有可接受的转换)

C2678 binary '==': no operator found which takes a left-hand operand of type 'Card' (or there is no acceptable conversion)

本文关键字:或者 运算符 转换 操作数 可接受 Card 二进制 类型 C2678      更新时间:2023-10-16

我收到由领带中断函数引起的上述错误

#include <vector>
#include <iostream>
#include <algorithm>
class Hand{
private:
std::vector<Card> cards;
public:
PokerHand(Card c1, Card c2) {
cards.push_back(c1);
cards.push_back(c2);
cards.push_back(c3);
cards.push_back(c4);
cards.push_back(c5);
}
bool breakTie(const PokerHand& other) const{
std::vector<Card> temp1 = { cards };
std::vector<Card> temp2 = { other.getCards()};
return std::find(temp1.begin(), temp1.end(), 2) <    std::find(temp2.begin(), temp2.end(), 2)
}
std::vector<Card> getCards(){
return cards;
}
};
class Card{
private:
int value;
public:
bool operator ==(const Card& right)const {
return (this->value == right.getValue());
}
int getValue(){
return value;
}
Card(int value){
this->value=value;
}
};
int main(){
Card a(4);
Card b(5);
Hand hand(a,b);
hand.breakTie();
return 0;
}

据我了解,该错误意味着我正在尝试更改 const 变量。我不明白的是我是如何更改常量变量的?

非常感谢帮助。

您正在使用整数2Cards 的向量上调用find。 您收到该错误是因为2不是Card,也没有任何Card的构造函数采用一个可以使用的整数参数(要么没有,要么存在的构造函数标记为explicit(。

你需要澄清(向编译器(你在寻找什么。 什么是Card2?

相关文章: