The the expression std::ostringstream() << speed.x
actually invokes the operator<<(double)
on the underlying base class std::ostream
interface.
The return type of std::ostream::operator<<(double)
is std::ostream&
which means you're trying to invoke the member function std::ostream::str()
which of course does not exist. That method is on the derived class.
This is why the static_cast
is necessary in this use case.
You could also write:
static_cast<std::ostringstream&>(std::ostringstream() << speed.x).str();
or since c++11
std::to_string(speed.x);
or in previous versions, you could write your own, less cryptic function which will do the same thing in a more maintainable way with no overhead.
std::string to_string(double x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}