我有一个简单的对象,我想在方法中返回一个对象。我知道使用的构造函数是有效的,因为它在其他地方使用过。
return Color(red, blue, green);
此代码返回以下错误:No matching constructor for initialization of 'transitLib::Color'
但是,只需添加即可*new
:
return *new Color(red, blue, green); //Valid, apparently.
知道为什么会产生这个错误吗?
附上类的完整代码
。H
class Color {
float red;
float blue;
float green;
public:
Color(float red, float blue, float green);
Color(Color &color);
Color colorByInterpolating(Color const& destinationColor, float fraction);
bool operator==(const Color &other) const;
bool operator!=(const Color &other) const;
Color operator=(const Color &other);
float getRed();
float getBlue();
float getGreen();
};
.cpp
transitLib::Color::Color(float red, float blue, float green):red(red),blue(blue),green(green){}
transitLib::Color::Color(Color &color):red(color.red),blue(color.blue),green(color.green){}
Color transitLib::Color::colorByInterpolating(Color const& destinationColor, float fraction) {
return Color(red + fraction * (destinationColor.red - red), blue + fraction * (destinationColor.blue - blue), green + fraction * (destinationColor.green - green));
}
bool Color::operator==(const Color &other) const {
return other.red == red && other.blue == blue && other.green == green;
}
bool Color::operator!=(const Color &other) const {
return !(other == *this);
}
Color Color::operator=(const Color &other) {
red = other.red;
blue = other.blue;
green = other.green;
return *this;
}
float transitLib::Color::getRed() {
return red;
}
float transitLib::Color::getBlue() {
return blue;
}
float transitLib::Color::getGreen() {
return green;
}