【发布时间】:2012-06-01 17:49:11
【问题描述】:
我正在尝试超载 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 是 typedeffor std::map<State*, std::multimap<QChar, Transition>>。 State 的标签为 QString,因此调用 .toStdString()。
在另一堂课上,我用machine 打电话给std::cout << machine->table << std::endl; TuringMachine*,这给了我
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
我做错了什么?为什么是&&?
编辑:使用 g++ 4.6 和 -std=c++0x
【问题讨论】:
-
toStdString 和 toAscii 返回什么类型?你知道错误信息是针对哪一行的吗?
-
toStdString()和toAscii()是QString和QChar的成员函数。它们分别返回std::string和char。错误发生在我调用std::cout << machine->table << std::endl的行中 -
这个
operator<<函数的声明是否从您尝试使用它的位置可见? (在同一文件的前面,或在#included 标头中) -
听起来你改变了一些东西。让我们与SSCCE 同步。
-
来自这里的答案:stackoverflow.com/questions/10651161/… 听起来很相似。出于某种原因,编译器可能不会考虑您的运算符。这让 aschepler 的建议听起来很不错!
标签: c++ stl operator-overloading multimap