0

クラスに問題があります。クラスの比較演算子を作成します。
いくつかのコード:

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 < function でコンパイル エラーが発生したことです。

return (float) *this < (float) other;

正しくは一部: (float) その他

エラーメッセージは次のとおりです。

cvariable.cpp|142|error: invalid cast from type 'const MCXJS::CVariable' to type 'float'|

問題の原因は何ですか?

4

2 に答える 2

4

変換演算子は const ではありませんが、other参照するオブジェクトは const 修飾されています。const次のように変換演算子に追加する必要があります。

operator int () const;
operator float () const;
operator std::string () const;

これconstも定義に追加する必要があります。

于 2010-09-24T17:29:50.077 に答える
0

純粋な推測。float 演算子は const ではありません

于 2010-09-24T17:30:45.707 に答える