Search code examples
c++stloperator-overloadingmultimap

A way to print of map of multimaps?


I'm trying to overload operator<<, and it drove me crazy:

std::ostream& operator<<(std::ostream & lhs, TuringMachine::TRTable& rhs){

    for(auto& statePtr : rhs){

        lhs << statePtr.first->getLabel().toStdString();
        for(auto& charPtr: statePtr.second){

            //lhs << '\t';
            lhs << charPtr.first.toAscii() ;
            //lhs << 'b ';
            lhs << charPtr.second.getState().getLabel().toStdString() << std::endl;
        }
    }

return lhs;
}

TRTable is a typedeffor std::map<State*, std::multimap<QChar, Transition>>. Statehas its label as a QString hence the call to .toStdString().

In another class I call std::cout << machine->table << std::endl; with machine beeing a TuringMachine* and this gives me

error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

What am I doing wrong? Why &&?

EDIT: using g++ 4.6 and -std=c++0x


Solution

  • In which namespace did you declare the operator<<? Since TRTable is a typedef ADL does not apply, so the operator<< is searched only in namespace std by ADL, since this is where the actual class is defined. So you might have to use the namespace where you defined the operator<< when you want to use it.