标题:死活也找不出哪儿出问题了?
只看楼主
hcs_xiaohan
Rank: 2
等 级:论坛游民
帖 子:40
专家分:23
注 册:2016-7-4
结帖率:91.67%
已结贴  问题点数:6 回复次数:3 
死活也找不出哪儿出问题了?
程序目的是计算圆的面积和表面积,代码有点长但是一点也不复杂, 请各位看看是哪儿出问题了?

#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;
搜索更多相关主题的帖子: include yellow public return 表面积 
2016-12-23 19:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:3 
template <typename T>
T& Circle<T>::area() {
    return M_PI * radius * radius;
}

返回类型应当是引用吗?
2016-12-23 20:22
hcs_xiaohan
Rank: 2
等 级:论坛游民
帖 子:40
专家分:23
注 册:2016-7-4
得分:0 
回复 2楼 rjsp
去掉应用就可以运行了, 但如果用引用的话main() 函数该怎么改?
2016-12-23 21:29
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:3 
从逻辑上来理解压根就不该返回引用。
2016-12-23 23:34



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-472838-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.573010 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved