gpt4 book ai didi

c++ - operator << 对象 vector 的重载

转载 作者:行者123 更新时间:2023-11-28 02:13:56 25 4
gpt4 key购买 nike

各位。

我有一个名为“Card”和“CardDeck”的类,其中第二个是 vector ,其中包含许多第一类的卡片。

我的 << 重载是这样的:

istream& operator<<(ostream& os, Card& card) {
string str;
if(cardValueCorrect(card._value)){
str += to_string(card._value);
} else {
str += card._identier;
}
str += suitToChar(card._suit);
return os << str;

而且我认为应该没问题,至少编译器不会提示这一点。当我想在我的 CardDeck 中重载 << 以便打印所有卡片时,问题就开始了。

CardDeck << overloading seems like that: 
ostream& operator<<(ostream& os, CardDeck& odeck){
for(const Card cur_card: odeck._Deck){
os << cur_card << ' ';
}
return os;
}

它提示访问卡的私有(private)字段,并且:

invalid initialization of reference of type 'std::istream& {aka std::basic_istream<char>&}' from expression of type 'std::basic_ostream<char>'

然后是其他非常奇怪的东西(关于第一个运算符重载中的每一行):

within this context

编辑

istream 到 ostream 已修复。

那是一个问题。现在我看到 162 条警告,其中包含关于 CardDeck 运算符重载的“信息”,它告诉我我正在进入 CardDeck 的私有(private)字段( vector 在那里)。

以太:

`invalid initialization of non-const reference of type 'CardDeck&' from an rvalue of type no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'const Card')` 

还有很多这种重复模式的“信息”( eclipse ):

'const Card' is not derived from 'const std::extreme_value_distribution<_RealType>'

最佳答案

替换

istream& operator<<(ostream& os, Card& card)

ostream& operator<<(ostream& os, Card& card)

关于c++ - operator << 对象 vector 的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com