私は単純なオブジェクトを持っていて、メソッドでオブジェクトを返したいと思っています。使用されているコンストラクターが他の場所で使用されているため、有効であることはわかっています。
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;
}