问题
I am struggling to get an appropriate return for this operator (it is not my code, just trying to correct it and I am not as good as I should be in C++ to correct it) can anybody help me with this, it is datatype class defined for high level design of digital circuits.
How to return this temp
without an error, is there any special approach to this?
inline friend std::ostream& operator << ( std::ostream& os, const sc_float &v)
{
if (c_DEBUG) std::cout << "debug: operator << called " << endl; //debug
// fixme - this is only copy of sc_float2double function
double temp;
temp = (double)v.man / exp2(m_width);
temp += 1.0;
temp *= exp2((double)v.exp - exp2((double)e_width - 1.0) + 1.0);
temp *= (v.sign == true ? -1.0 : 1.0);
//os << "(" << v.sign << " , " << v.exp << " , " << v.man << ")"; // debug
os << temp;
}
as I add there return os;
I got 226 errors which points to systemC library and instances there. Has anybody done declaration of stream operator with regards to systemC classes or have anybody idea how it is done?
回答1:
Your function is missing its return. The <<
operator should return a reference to the stream that it was using so that you can chain operations together like
cout << foo << bar << foobar;
To fix your function you just need to return the ostream
that you are using in your function
inline friend std::ostream& operator << ( std::ostream& os, const sc_float &v)
{
//...
os << temp;
return os;// <-- this returns the stream that we are unsing so it can be used by other functions
}
来源:https://stackoverflow.com/questions/30401691/error-c4716-operator-must-return-a-value