I have a problem with return type of my class operator overload. I want to return the same object when operator does it's thing so i can continue the sequence. I have a class Console and this code:
template <class out>
Console& Console::operator<<(const out &data)
{
std::stringstream ss;
ss << data;
output = ss.str();
return this;
}
but the error I get is
UI_Console.cpp: In member function 'vui::Console& vui::Console::operator<<(const out&)': UI_Console.cpp:13:12: error: invalid initialization of non-const reference of type 'vui::Console&' from an rvalue of type 'vui::Console*'
I can make it void but when I call an operator on object like:
Console obj;
obj << "Hello" << "World\n";
I get an error that the second operator doesn't know what to do. How can I make it work?
CodePudding user response:
You return this
as a pointer. Your function declaration requires a reference, thus you need return *this;
.