死活也找不出哪儿出问题了?
程序目的是计算圆的面积和表面积,代码有点长但是一点也不复杂, 请各位看看是哪儿出问题了?#include <string>
#include <cmath>
#include <iostream>
using namespace std;
enum eColor {None, green, yellow, blue, red, black, white};
class Color {
protected:
eColor mColor;
public:
Color(eColor _Color);
void setColor(eColor Color);
eColor getColor() {return mColor;}
string getStrColor();
};
Color::Color(eColor _Color) {
mColor = _Color;
}
void Color::setColor(eColor Color) {
mColor = Color;
}
string Color::getStrColor() {
switch(mColor) {
case green: return "green";
case yellow: return "yelloe";
case blue: return "blue";
case red: return "red";
case black: return "black";
case white: return "white";
default: return "none";
}
}
template <typename T>
class Circle: public Color {
protected:
T x;
T y;
T radius;
public:
Circle(T const & _x, T const & _y, T const & _radius, eColor color);
Circle(T const & _x, T const & _y, T const & _radius);
Circle(T const & _radius);
T & area() ;
T & circumference() ;
T & getx() {return x;}
T & gety() {return y;};
T & getradius() {return radius;}
};
template <typename T>
Circle<T>::Circle(T const & _x, T const & _y, T const & _radius, eColor color): Color(color) {
x = _x;
y = _y;
radius = _radius;
}
template <typename T>
Circle<T>::Circle(T const & _x, T const & _y, T const & _radius) {
x = _x;
y = _y;
radius = _radius;
}
template <typename T>
Circle<T>::Circle(T const & _radius) {
x = const_cast<T>(0);
y = const_cast<T>(0);
radius = _radius;
}
template <typename T>
T & Circle<T>::area() {
return M_PI * radius * radius;
}
template <typename T>
T & Circle<T>::circumference() {
return const_cast<T>(2) * M_PI * radius;
}
int main() {
Circle<float> demo(0.0, 0.0, 10.0, white);
cout << "area:" << demo.area() << endl;
cout << "color:" << demo.getStrColor() << endl;
return 0;
}
这是报错信息:
test.cpp: In instantiation of ‘T& Circle<T>::area() [with T = float]’:
test.cpp:96:34: required from here
test.cpp:85:28: error: invalid initialization of non-const reference of type ‘float&’ from an rvalue of type ‘float’
return M_PI * radius * radius;