我正在尝试重载operator<<
,这让我发疯:
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
是 typedef
对于 std::map<State*, std::multimap<QChar, Transition>>
。 State
其标签为 QString
因此调用.toStdString()
.
在另一个类上我调用 std::cout << machine->table << std::endl;
与 machine
成为TuringMachine*
这给了我
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
我做错了什么?为什么&&
?
编辑:使用 g++ 4.6 和 -std=c++0x
最佳答案
您在哪个命名空间中声明了 operator<<
?自 TRTable
是一个 typedef ADL不适用,所以 operator<<
仅在 namespace std
中搜索由 ADL 定义,因为这是定义实际类的地方。所以你可能必须use
您定义 operator<<
的命名空间当你想使用它时。
关于c++ - 打印多 map map 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766381/