我遇到了我的课的问题。我要去做我班的比较操作员。
一些代码:无效投入类型'浮动'
CVariable::operator float()
{
float rt = 0;
std::istringstream Ss (m_value);
Ss >> rt;
return rt;
};
bool CVariable::operator < (const CVariable& other)
{
if (m_type == STRING || other.Type() == STRING)
int i = 0; // placeholder for error handling
else
return (float) *this < (float) other;
};
类声明:
class CVariable
{
public:
inline VARTYPE Type() const {return m_type;};
inline const std::string& Value() const {return m_value;};
bool SetType (VARTYPE);
private:
int m_flags;
VARTYPE m_type;
std::string m_value;
public:
// ...
operator int();
operator float();
operator std::string();
//...
inline bool operator == (const CVariable& other) {return m_value == other.Value();};
inline bool operator != (const CVariable& other) {return m_value != other.Value();};
bool operator < (const CVariable&);
问题是,我有编译错误的操作<功能,在这条线:
return (float) *this < (float) other;
在正确部分:(浮动)其他
呃ror的讯息是:
cvariable.cpp|142|error: invalid cast from type 'const MCXJS::CVariable' to type 'float'|
问题的原因是什么?
修复代码时,请考虑使用static_cast(* this)而不是C样式(float)* this'。 –
2010-09-24 17:32:17