[英]C++ ostream operator << overloading
I have a Card with parameters (Color, Value) and method to output Card's parameters. 我有一张带有参数(颜色,值)和输出卡参数的方法的卡。
Error binary '<<' : no operator found which takes a right-hand operand of type 'CardColor' and 'CardValue'(or there is no acceptable conversion) 错误二进制“ <<”:找不到使用“ CardColor”和“ CardValue”类型的右侧操作数的运算符(或者没有可接受的转换)
Card.h 卡h
struct Card{
Color CardColor;
Value CardValue;
Card(Color color, Value cardValue) {
this->CardColor = color;
this->CardValue = cardValue;
}
public:
friend std::ostream &operator<<(std::ostream &output, const Card c);
};
Value.h 值h
#ifndef VALUE_H
#define VALUE_H
enum class Value{ SEDMA, OSMA, DEVITKA, DESITKA, SPODEK, KRAL, ESO, SVRSEK };
#endif
Color.h 颜色.h
#ifndef COLOR_H
#define COLOR_H
enum class Color { ZALUD, LISTY, SRDCE, KULE };
#endif
Main.cpp Main.cpp
#include <Card.h>
std::ostream &operator<<(std::ostream &output, const Card c)
{
output << c.CardColor << c.CardValue << std::endl;
return output;
}
void outputCard(Card c)
{
std::cout << c << std::endl;
}
From you comment: 来自您的评论:
binary '<<' : no operator found which takes a right-hand operand of type 'CardColor' and 'CardValue' (or there is no acceptable conversion)
二进制'<<':找不到使用'CardColor'和'CardValue'类型的右侧操作数的运算符(或者没有可接受的转换)
You also need to provide operator<<
for the Color and Value class. 您还需要为Color和Value类提供
operator<<
。
example of this for the Color class: Color类的示例:
#ifndef COLOR_H
#define COLOR_H
#include <iostream>
enum class Color { ZALUD, LISTY, SRDCE, KULE };
inline std::ostream& operator<<(std::ostream& os, const Color& col)
{
switch (col) {
case ZALUD :
os << "ZALUD";
break;
// ...
}
return os;
}
#endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.