C++ ostream 运算符<<重载
C++ ostream operator << overloading
我有一张带有参数(颜色、值)和输出卡参数的方法的卡片。
错误二进制"<<":未找到采用类型为"CardColor"和"CardValue"的右侧操作数的运算符(或者没有可接受的转换)
卡.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);
};
值.h
#ifndef VALUE_H
#define VALUE_H
enum class Value{ SEDMA, OSMA, DEVITKA, DESITKA, SPODEK, KRAL, ESO, SVRSEK };
#endif
颜色.h
#ifndef COLOR_H
#define COLOR_H
enum class Color { ZALUD, LISTY, SRDCE, KULE };
#endif
主.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;
}
来自您的评论:
二进制"<<":未找到采用类型为"CardColor"和"CardValue"的右侧操作数的运算符(或者没有可接受的转换)
还需要为"颜色"和"值"类提供operator<<
。
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
相关文章:
- 逗号在重载 ostream 和 istream 在结构中分开
- 重载 ostream << 运算符,指针作为参数,导致输出上的内存地址
- 使用shared_ptr向量重载 ostream 友元运算符
- 使用向量重载 ostream 运算符
- C++以友元身份重载 ostream 运算符会导致错误:变量在此上下文中是私有的
- C++ 重载 ostream 不适用于带有空参数的默认构造
- 为什么重载 ostream 的运算符<<需要一个引用"&"?
- C++创建一个标头,其中包含用于重载 ostream 的模板
- 是否可以为算术表达式重载 ostream 运算符
- 运算符重载:Ostream/Istream
- C++:在重载 ostream >> 时引用 2D 数组元素时崩溃
- 在重载的后缀递增/递减运算符上重载ostream运算符
- 具有重载 ostream <<运算符的递归类模板
- 重载ostream&operator<<:如何强制输出零?
- 重载 ostream << 运算符
- 派生成员和重载Ostream运算符
- C++重载 ostream <<以进行任意收集
- 重载ostream&时转换错误无效;operator< & lt;
- 重载ostream操作符将如何解释重载操作符的表达式?
- 在一个类上重载 ostream <<并输出另一个类的数据