1

I want to overload << operator. Here is my code:

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <list>
using namespace std;

enum class Zustand{Neuwertig,Gut,Abgegriffen,Unbrauchbar};

class Exemplar{
private:
    int aufl_num;
    Zustand z;
    bool verliehen;

public:
    Exemplar(int aufl_num);
    Exemplar(int aufl_num,Zustand z);
    bool verfuegbar() const;
    bool entleihen();
    void retournieren(Zustand zust);
    friend ostream& operator<<(ostream& os, const Exemplar& Ex);
};

//Constructor 1;
Exemplar::Exemplar(int aufl_num):
    aufl_num{aufl_num},
    z{Zustand::Neuwertig},
    verliehen{false}
    {
        if(aufl_num >1000 || aufl_num <1) throw runtime_error("Enter correct number betwee 1 and 1000");

    }

// Constructor 2;
Exemplar::Exemplar(int aufl_num,Zustand z):
    aufl_num{aufl_num},
    z{z},
    verliehen{false}
    {
        if(aufl_num >1000 || aufl_num <1) throw runtime_error("Enter correct number betwee 1 and 1000");

    }


ostream& operator<<(ostream& os, const Exemplar& Ex){
    if(Ex.verliehen == true){
        os << "Auflage:" << Ex.aufl_num <<","<< "Zustand:"<<Ex.z<<","<<"verliehen";
    }else{
        os << "Auflage:" << Ex.aufl_num <<","<< "Zustand:"<<Ex.z;
    }

}

I declared my ostream& operator<< as a friend function, definitions inside class and function code seemse to be the same. But I have no idea, why compiler throws me an error "error: no match for ‘operator<<’. Can you help me to figure out where is the problem?

Error message:

main.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Exemplar&)’:
main.cpp:72:53: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const Zustand’)
   os << "Auflage:" << Ex.aufl_num <<","<< "Zustand:"<<Ex.z<<","<<"verliehen";
Rob_Fir
  • 155
  • 1
  • 7
  • 2
    Please include the full error message, it will say what types the compiler was trying to call `operator<<` with. – Richard Critten Feb 05 '18 at 11:35
  • 1
    [mcve], please. The code you show doesn't even attempt to call that `operator<<`. Beyond that, try to remove unrelated details (but keep the code compilable with *just that error you ask about*). – StoryTeller - Unslander Monica Feb 05 '18 at 11:35
  • 4
    I guess the compiler is complaining about the missing `operator <<` for type `Zustand`. – O'Neil Feb 05 '18 at 11:36
  • Look closely at your error message. The problem is not with the `<<` overload for your `Exemplar` class, but the fact that no `<<` overload is defined for your enumeration class. – Sam Varshavchik Feb 05 '18 at 11:36
  • Doesn't you error complain about outputting `Ex.z`, which is an `enum class`? See https://stackoverflow.com/questions/11421432/how-can-i-output-the-value-of-an-enum-class-in-c11 – Daniel Langr Feb 05 '18 at 11:36

2 Answers2

6

Your error is quite simple. You try to call: os << Ex.z, where z is a Zustand, and that Zustand has no ostream operator implemented for it. You may want to print out the value of that enum as an integer. The compiler doesn't know how to print out a Zustand, so you have to tell it how to.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • But in order to print a word(instead of integer), should I overload << operator for enum class as well? – Rob_Fir Feb 05 '18 at 11:41
  • Yes, you have to do that. However, you will have to get a corresponding string for each enum value, since there is no way to cast it. – Arnav Borborah Feb 05 '18 at 11:43
  • @Rob_Fir Yes. The *names* "Neuwertig, Gut, Abgegriffen, Unbrauchbar" do not appear *as strings* in your program, you have to have some strings with those values *somewhere* if you want them – Caleth Feb 05 '18 at 11:44
  • 2
    @Rob_Fir If you have multiple enums where you want to associate the text of the name with each value, [have a look here](https://codereview.stackexchange.com/a/14315/82515) for an implementation – Caleth Feb 05 '18 at 11:51
1

Change

os << "Auflage:" << Ex.aufl_num <<","<< "Zustand:"<<Ex.z<<","<<"verliehen";

into

os << "Auflage:" << Ex.aufl_num <<","<< "Zustand:"
   << static_cast<std::underlying_type<Zustand>::type>(Ex.z) 
   <<","<<"verliehen";

and similarly the second line.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93