C++ 我在a.h中有一个枚举类,在a.cpp中有一个运算符重载错误:与‘;操作员<<’;
操作系统C++ 我在a.h中有一个枚举类,在a.cpp中有一个运算符重载错误:与‘;操作员<<’;,c++,operator-overloading,enum-class,C++,Operator Overloading,Enum Class,操作系统 #ifndef OPERATING_SYSTEM_H #define OPERATING_SYSTEM_H #include <iostream> enum class OperatingSystem { unknown, android, iOS, macOS, Linux, propietary, Unix, windows }; #endif \ifndef操作系统\u H #定义操
#ifndef OPERATING_SYSTEM_H
#define OPERATING_SYSTEM_H
#include <iostream>
enum class OperatingSystem
{
unknown,
android,
iOS,
macOS,
Linux,
propietary,
Unix,
windows
};
#endif
\ifndef操作系统\u H
#定义操作系统
#包括
枚举类操作系统
{
不详,
安卓
网间网操作系统,
马科斯,
Linux,
丙,
Unix,
窗户
};
#恩迪夫
OperatingSystem.cpp
#include "OperatingSystem.h"
std::ostream& operator<< (std::ostream& os,
OperatingSystem OS)
{
switch (OS)
{
case OperatingSystem::unknown : os << "unknown OS";
break;
case OperatingSystem::android : os << "Android OS";
break;
case OperatingSystem::iOS : os << "iOS";
break;
case OperatingSystem::macOS : os << "MacOS";
break;
case OperatingSystem::Linux : os << "Linux OS";
break;
case OperatingSystem::propietary : os << "proprietary OS";
break;
case OperatingSystem::Unix : os << "Unix OS";
break;
case OperatingSystem::windows : os << "MS Windows OS";
break;
}
return os;
}
#包括“OperatingSystem.h”
std::ostream&operator您必须提供operator的声明导致此错误的原因是,默认情况下,不允许运算符使用用户定义的类型(例如对象、结构、枚举类)。因此,我们对输出流操作符进行了操作符重载,该操作符将枚举类作为参数
声明头文件中输出流运算符的重载
std::ostream& operator<< (ostream&, const OperatingSystem&)
std::ostream&operator请参见OperatingSystem.h和OperatingSystem.cpp,但似乎缺少Device.cpp。在使用之前,需要了解所有函数。对于名为“operator”的函数也不例外
std::ostream& operator<< (ostream&, const OperatingSystem&)
include "OperatingSystem.h"
std::ostream& operator<<(ostream& o, const OperatingSystem& os)
{
// implementation
o << "example";
return o;
}