编写代码时,我在C ++中出现错误:错误1错误C2601:'circle':局部函数定义不合法

时间:2018-10-29 16:21:53

标签: c++

我在编写此代码时在C ++中遇到问题:

error C2601: 'circle' : local function definitions are illegal

如何找到解决问题的方法?

#include < iostream > 
using namespace std;
int main() {
    char grade;
    cout << "enter grade : ";
    cin >> grade;
    switch (grade) {
    case 'A':
        void circle(float &, float &, float);
        {
            float r, a, c;
            cout << "enter radius:";
            cin >> r;
            circle(a, c, r);
            cout << "area=" << a << "\t" << "circumference=" << c << endl;
            return 0;
        }
        void circle(float & area, float & circumference, float r)
        {
            const double pi = 3.14159;
            area = pi * r * r;
            circumference = 2 * pi * r;
        }
        break;
    default:
        cout << "Invalid grade" << endl;
    }
    cout << "Your grade is " << grade << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:4)

它说了什么。在main函数之外定义函数(和原型!)。

#include < iostream > 

void circle(float & area, float & circumference, float r)
{
    const double pi = 3.14159;
    area = pi * r * r;
    circumference = 2 * pi * r;
}

using namespace std;
int main() {
    char grade;
    cout << "enter grade : ";
    cin >> grade;
    switch (grade) {
    case 'A':
        {
            float r, a, c;
            cout << "enter radius:";
            cin >> r;
            circle(a, c, r);
            cout << "area=" << a << "\t" << "circumference=" << c << endl;
            return 0;
        }
        break;
    default:
        cout << "Invalid grade" << endl;
    }
    cout << "Your grade is " << grade << endl;

    return 0;
}