gpt4 book ai didi

c++ - 一种打印多 map map 的方法?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:55:14 30 4
gpt4 key购买 nike

我正在尝试重载 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;
}

TRTabletypedef对于 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<< 的 namespace 当你想使用它时。

关于c++ - 一种打印多 map map 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10766381/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com