重载乘法运算符失败
Overloading multiplication operator fails
我正在尝试为 AVR 项目编写 RGB Class。
但是当我尝试使用它时,它在某些操作员处失败了。
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;
}
我想做的是(错误中的第 321 行):
RGB newColor;
newColor = v * m_color + (1 - v) * m_targetColor;
而 V 是某个值 [0,1]
。
我做错了什么?
您的会员运营商
RGB &operator* (const float &other);
相当于非成员运算符:
RGB& operator* (RGB, float);
这处理左手乘法:RGB() * float()
。您需要添加第二个非成员 operator*
来处理右手乘法:
RGB operator*(float, const RGB& );
请注意,operator*
应该 return 一个 RGB
,而不是 RGB&
,因为您当前 return 正在引用临时 RGB
.
你的二元运算符 *
运算符重载采用 RGB 类型的 rhs 值应该是一个非成员函数,因为你已经为 RGB 重载了运算符,但是没有为 float[=14 定义该方法=]
转换
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;
}
并将以上加为好友
备注
更好的设计是只为您的 class 重载一元运算符,并将所有二元运算符设为非成员。
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;
}
我正在尝试为 AVR 项目编写 RGB Class。 但是当我尝试使用它时,它在某些操作员处失败了。
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;
}
我想做的是(错误中的第 321 行):
RGB newColor;
newColor = v * m_color + (1 - v) * m_targetColor;
而 V 是某个值 [0,1]
。
我做错了什么?
您的会员运营商
RGB &operator* (const float &other);
相当于非成员运算符:
RGB& operator* (RGB, float);
这处理左手乘法:RGB() * float()
。您需要添加第二个非成员 operator*
来处理右手乘法:
RGB operator*(float, const RGB& );
请注意,operator*
应该 return 一个 RGB
,而不是 RGB&
,因为您当前 return 正在引用临时 RGB
.
你的二元运算符 *
运算符重载采用 RGB 类型的 rhs 值应该是一个非成员函数,因为你已经为 RGB 重载了运算符,但是没有为 float[=14 定义该方法=]
转换
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;
}
并将以上加为好友
备注
更好的设计是只为您的 class 重载一元运算符,并将所有二元运算符设为非成员。
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;
}