error: expected a ';', bug in Intellisense when building?

error: expected a ';', bug in Intellisense when building?

我在 Visual Studio 2019 年同时构建和使用 IntelliSense 时遇到错误,它说 expected a ';'(代码:E0065,错误应该在第 9 行), 我非常困惑,因为这是我第一次在定义函数时遇到这样的错误,因为这也是我第一次在 c++ 中定义函数,我真的不知道我在哪里可以漏掉一个分号。该程序没有 运行.

#include <iostream>

using namespace std;

int main() {
    
    const double pi{3.14159};

    double calc_area_circle(double radius) {
        return pi * (radius * radius);
    }

    void area_circle() {
        double radius{};
        cout << "Enter the radius of the circle: ";
        cin >> radius;
        cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
    }

    return 0;
}

如果有人能帮我解决这个问题,我将不胜感激。

在 C++ 中不允许嵌套函数定义(在另一个函数中定义函数),除非您使用 lambda 函数。

将函数定义放在函数外

#include <iostream>

using namespace std;

const double pi{3.14159};

double calc_area_circle(double radius) {
    return pi * (radius * radius);
}

void area_circle() {
    double radius{};
    cout << "Enter the radius of the circle: ";
    cin >> radius;
    cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
}

int main() {
    return 0;
}

或将它们转换为 lambda 函数

#include <iostream>

using namespace std;

int main() {
    
    const double pi{3.14159};

    const auto calc_area_circle = [&](double radius) -> double {
        return pi * (radius * radius);
    };

    const auto area_circle = [&]() {
        double radius{};
        cout << "Enter the radius of the circle: ";
        cin >> radius;
        cout << "The area of the circle with radius " << radius << " is " << calc_area_circle(radius) << endl;
    };

    return 0;
}

注意:这些程序将不执行任何操作,因为未调用定义的函数。