неверное приведение к типу 'float' - PullRequest
0 голосов
/ 24 сентября 2010

У меня проблемы с моим классом.Я собираюсь сделать операторы сравнения моего класса.
Некоторый код:

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&);

Проблема в том, что у меня ошибка компиляции воператор <функция, в этой строке: </p>

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

Правильно частично: (плавать) другое

Сообщение об ошибке:

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

В чем причина проблемы

Ответы [ 2 ]

4 голосов
/ 24 сентября 2010

Ваш оператор преобразования неконстантный, но объект, на который ссылается other, является константным. Вы должны добавить const к операторам преобразования следующим образом:

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

Это const также необходимо добавить к определениям.

0 голосов
/ 24 сентября 2010

чистое предположение. Вы плавающий оператор не констант

...