1

OperatingSystem.h

#ifndef OPERATING_SYSTEM_H
#define OPERATING_SYSTEM_H

#include <iostream>

enum class OperatingSystem
{
    unknown,
    android, 
    iOS, 
    macOS, 
    Linux, 
    propietary, 
    Unix, 
    windows 
};

#endif

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

Error

Device.cpp:17:68: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘OperatingSystem’) std::cout << get_model() << ", RAM " << _main_memory << "GB, " << get_os();

2
  • I see OperatingSystem.h and OperatingSystem.cpp but Device.cpp appears to be missing. Commented Feb 5, 2020 at 13:40
  • All functions need to be known before you can use them. There is no exception for functions with funky names like "operator <<". Commented Feb 5, 2020 at 14:58

2 Answers 2

1

You have to provide a declaration of operator<< in the header file:

std::ostream& operator<< (std::ostream& os, 
OperatingSystem OS);

The error message is from another translation unit (Device.cpp) that does not include OperatingSystem.cpp but only the header file OperatingSystem.h. The OperatingSystem.h file does not contain a declaration of the function that you are trying to call. As a result, the function is missing in the Device.cpp translation unit.

Once you've provided that declaration, the compiler knows that such a function exists and that the linker will be able to resolve it later on if invoked correctly.

Sign up to request clarification or add additional context in comments.

Comments

1

The error is caused because operators, by default,are not allowed to work with user-defined types(e.g objects, structures, enum class). Thus, we have do an operator overloading of output stream operator that takes an enum class as an argument.

Declare the the overloading of output stream operator in header file

std::ostream& operator<< (ostream&, const OperatingSystem&)

Import the headerFile and Define the overloading of output stream operator

include "OperatingSystem.h"
std::ostream& operator<<(ostream& o, const OperatingSystem& os)
{
  // implementation
  o << "example";
  return o;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.