重载乘法运算符失败

时间:2015-04-27 14:08:12

标签: c++ operator-overloading

我正在尝试为AVR项目编写RGB类。 但是当我尝试使用它时,它在某些运营商处失败了。

Error   415 no match for 'operator*' (operand types are 'const float' and 'RGB')    ...\Animation\Wall\Wall.cpp 321 18  Cube

以下是它的外观:

class RGB
{
//variables
public:
    uint8_t r, g, b;
//functions
public:
    RGB();
    //assignment
    RGB &operator=( const RGB &other );

    RGB &operator+(const RGB &other);

    RGB &operator* (const uint8_t &other);
    RGB &operator* (const float &other);
//some more operators here
}; //RGB

实施:

RGB::RGB(): r(0), g(0), b(0)
{
}

RGB &RGB::operator=( const RGB &other)
{
    if(this != &other) //no self assignment
    {
        r = other.r;
        g = other.g;
        b = other.g;
    }
    //per convention return "yourself"
    return *this;
}

RGB &RGB::operator+(const RGB &other)
{
    r += other.r;
    r %= MAX_COLOR_RGB;
    g += other.g;
    g %= MAX_COLOR_RGB;
    b += other.b;
    b %= MAX_COLOR_RGB;
    //per convention return "yourself"
    return *this;
}

RGB &RGB::operator*(RGB rgb, const uint8_t &i)
{
    rgb.r *= i;
    rgb.r %= MAX_COLOR_RGB;
    rgb.g *= i;
    rgb.g %= MAX_COLOR_RGB;
    rgb.b *= i;
    rgb.b %= MAX_COLOR_RGB;
    //per convention return "yourself"
    return *this;
}

RGB &RGB::operator*( const float &f, RGB rgb)
{
    rgb.r *= f;
    rgb.r %= MAX_COLOR_RGB;
    rgb.g *= f;
    rgb.g %= MAX_COLOR_RGB;
    rgb.b *= f;
    rgb.b %= MAX_COLOR_RGB;
    return rgb;
}

RGB &RGB::operator*(const uint8_t &i, RGB rgb)
{
    rgb.r *= i;
    rgb.r %= MAX_COLOR_RGB;
    rgb.g *= i;
    rgb.g %= MAX_COLOR_RGB;
    rgb.b *= i;
    rgb.b %= MAX_COLOR_RGB;
    //per convention return "yourself"
    return rgb;
}

RGB &RGB::operator*(RGB rgb, const float &f)
{
    rgb.r *= f;
    rgb.r %= MAX_COLOR_RGB;
    rgb.g *= f;
    rgb.g %= MAX_COLOR_RGB;
    rgb.b *= f;
    rgb.b %= MAX_COLOR_RGB;
    return rgb;
}

Whan我正在尝试todo(来自错误的第321行):

RGB newColor;
newColor = v * m_color + (1 - v) * m_targetColor;

V是一些值[0,1]。 我做错了什么?

2 个答案:

答案 0 :(得分:7)

您的会员运营商

RGB &operator* (const float &other);

等同于非成员运算符:

RGB& operator* (RGB, float);

这会处理左侧乘法:RGB() * float()。您需要添加第二个非成员operator*来处理右侧乘法:

RGB operator*(float, const RGB& );

请注意,operator*应该返回RGB,而不是RGB&,因为您当前正在返回对临时RGB的引用。

答案 1 :(得分:1)

你的二进制运算符*运算符重载采用RGB类型的rhs值应该是非成员函数,因为你已经为RGB运算符重载了,但是没有为float定义该方法

转换

RGB &RGB::operator*( const float &f, RGB rgb)
{
    rgb.r *= f;
    rgb.r %= MAX_COLOR_RGB;
    rgb.g *= f;
    rgb.g %= MAX_COLOR_RGB;
    rgb.b *= f;
    rgb.b %= MAX_COLOR_RGB;
    return rgb;
}

致非会员职能

RGB operator*( const float &f,const  RGB& rgb)
{
    RGB _rgb = rgb;
    _rgb = _rgb * f;
    return _rgb;
}

并将上述作为朋友

注意

更好的设计是仅为您的类重载一元运算符,并使所有二元运算符成为非成员。

RGB& RGB::operator*( const float &f)
{
    r = (r * f) % MAX_COLOR_RGB;
    g = (g * f) % MAX_COLOR_RGB;
    b = (b * f) % MAX_COLOR_RGB;
    return *this;
}

RGB operator*( const float &f,const RGB& rgb)
{
    RGB _rgb = rgb;
    _rgb = _rgb * f;
    return _rgb;
}

RGB operator*( const RGB& rgb, const float &f)
{
    RGB _rgb = rgb;
    _rgb = _rgb * f;
    return _rgb;
}