ofstream& operator<<(ostream &outStream, const EventClass &eventObject)
{
outStream << eventObject.getEventName() << " event at "
<< eventObject.getEventTime() << endl;
return(outStream);
}
我相信這段代碼足以分析錯誤。運算符<< overloading「錯誤:傳遞'const ....」
當我編譯我的代碼,我得到了以下錯誤:
error: passing ‘const EventClass’ as ‘this’ argument of ‘std::string EventClass::getEventName()’ discards qualifiers [-fpermissive]
outStream << eventObject.getEventName() << " event at "error: passing ‘const EventClass’ as ‘this’ argument of ‘int EventClass::getEventTime()’ discards qualifiers [-fpermissive]
<< eventObject.getEventTime() << endl;error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream&}’ from expression of type ‘std::ostream {aka std::basic_ostream}’
return(outStream);
任何想法如何解決這些錯誤?
getEventName方法標記爲const嗎? –
不,它不是。它是否必須是const來解決這個問題? – nm17
是的,它必須是const來解決問題。 – kfsone