Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我在a.h中有一个枚举类,在a.cpp中有一个运算符重载错误:与‘;操作员<<’;_C++_Operator Overloading_Enum Class - Fatal编程技术网

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;
}