KrapnixTheFootballer
KrapnixTheFootballer

Reputation: 169

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

I got an error in Visual Studio 2019, when building and using IntelliSense at the same time, which said that it expected a ';' (CODE: E0065 and the error is supposed to be on line 9), I am extremely confused because this is my first time getting an error like this when defining functions because it is also my first time defining functions in c++, I really don't know where I could be missing a semi colon. The program doesn't run.

#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;
}

If any one could clear this up for me, I would really appreciate it.

Upvotes: 0

Views: 281

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

Nesting of function definition (defining functions inside another functions) is not allowed in C++ unless you use lambda functions.

Put the function definitions outside the function

#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;
}

or convert them to lambda function

#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;
}

Note: these programs will do nothing because the defined functions are not called.

Upvotes: 1

Related Questions