Overload output operator in an enumeration in C ++

Is it possible to overload the output statement inside an enumeration? I get all kinds of errors (if I use a class, I don't get it):

   ../src/oop.cpp:18:2: error: expected identifier before 'friend'
    ../src/oop.cpp:18:2: error: expected '}' before 'friend'
    ../src/oop.cpp:18:2: error: 'friend' used outside of class
    ../src/oop.cpp:18:16: error: expected initializer before '&' token
    ../src/oop.cpp:22:1: error: expected declaration before '}' token

I need to implement something like this (java code):

public enum Type { 

  ACOUSTIC, ELECTRIC;

  public String toString() {
    switch(this) {
      case ACOUSTIC: return "acoustic";
      case ELECTRIC: return "electric";
      default:       return "unspecified";
    }
  }
}

Thank.

edit:

enum Type {
    //ACOUSTIC, ELECTRIC;
    inline std::ostream& operator << (std::ostream& os, Type t) // error here
    {

    }
};
+3
source share
1 answer
enum MyEnum {
  foo, bar
};

inline std::ostream & operator<<(std::ostream & Str, MyEnum V) {
  switch (V) {
  case foo: return Str << "foo";
  case bar: return Str << "bar";
  default: return Str << (int) V;
}
+5
source

All Articles