0

I'm using a state machine to learn C++ and I want to provide an overloaded operator << to return the respective string instead of an int. Apologies for the length ...

#ifndef STATEMACHINE_H
#define STATEMACHINE_H

#include <map>
#include <string>

namespace statemachine{
    using namespace std;

    enum State { ON, RESTING, SLEEPING, LOCKED, OFF };

    struct StateMap : map<unsigned int, string>
    {
        StateMap()
        {
            this->operator[]( ON ) = "ON";
            this->operator[]( RESTING ) = "RESTING";
            this->operator[]( SLEEPING ) = "SLEEPING";
            this->operator[]( LOCKED ) = "LOCKED";
            this->operator[]( OFF ) = "OFF";
        };

        ~StateMap(){};
    };

struct Machine {

    Machine(State state) : statemap() {
        m_currentstate = state;
    }

    //  trying to overload the operator -- :(
    //  Error   1   error C2676: binary '<<' : 'std::ostream' does not define this operator or a 
    //  conversion to a type acceptable to the predefined operator **file** 38 1 statemachine_01
    ostream& operator << (ostream& stream){
        stream << statemap[m_currentstate];
        return stream;
    }

    State state() const {
        return m_currentstate;
    }

    void set_state(State state){
        m_currentstate = state;
    }

private:
    State m_currentstate;
    StateMap statemap;
};

}

#endif

What am I doing incorrectly?

IAbstract
  • 19,551
  • 15
  • 98
  • 146

2 Answers2

0

You are defining operator << to be a member of Machine. That would mean that it has to be called like this:

machine << stream;

instead of:

stream << machine;

You need to define the operator as a free function, to be able to take the arguments in the right order. For instance, you could make it be a friend function:

friend ostream& operator << (ostream& stream, Machine const& m){
    stream << m.statemap[m.m_currentstate];
    return stream;
}
K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • By *You need to define the operator as a free function* ...you mean in my header file, not within a struct? Sorry, I'm new to C++ and trying to get a grasp of how things are working. I'm experienced with C#. – IAbstract May 26 '12 at 18:12
  • @IAbstract: A free function is a function that is not a member of a class. If you replace your `operator <<` definition with mine it should just work. You may start getting duplicate linker errors for defining a free function at a header file, to avoid them declare the function `inline` (or move its definition to a .cpp file). – K-ballo May 26 '12 at 18:23
  • I found a solution that explains more detial: http://stackoverflow.com/a/9230853/210709 – IAbstract May 26 '12 at 19:07
0

It doesn't have to do anything with overloading (well, somewhat perhaps).

This statement

stream << statemap[m_currentstate];

fails, as you have not defined how to apply a StateMap to <<.

[You must overload << in the statemap class, too.]

Sorry, you need a standalone function, not a method, see other answer.

JohnB
  • 13,315
  • 4
  • 38
  • 65