我的课有问题。我要为我的班级制作比较运算符。
一些代码:
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&);
问题是,我在 operator < 函数中有编译错误,在这一行:
return (float) *this < (float) other;
部分正确:(浮动)其他
错误信息是:
cvariable.cpp|142|error: invalid cast from type 'const MCXJS::CVariable' to type 'float'|
问题的原因是什么?