简体   繁体   中英

Compile Error: Function does not take 1 arguments

void displayCost();
double computeArea();
double roundCost();

int main()
{
    return 0;
}

void displayCost(string name, int size)
{
    double area = computeArea(size);

    cout << "Since a " << size << "-inch pizza covers" << area << "square inches, "
         << name << ", then a 12 cents per square inch, the cost will be" << 12*area
         << " - which rounds to" << roundCost();
}

double computeArea(int size)
{
    double radius = size/2;
    double area = pi*radius*radius;
    return area;
}

double roundCost(double price)
{
    double roundedCost = ceil(price*100)/100;
    return roundedCost;
}

It happens on the line at double area = computeArea(size); . I don't understand why it says I'm not passing an argument in when I clearly am.

double computeArea();
double area = computeArea(size);
double computeArea(int size) {

One of these things is not like the others, .. .

You need to fix your prototype (the first one) to match the actual function:

double computeArea(int);

And, of course, ditto for the other prototypes as well.

You are messing up your forward declarations, they also have to reflect the type of parameters of your function.

void displayCost(string, int);
double computeArea(int);
double roundCost(double);

C++ differs from C in that a forward declaration of a function must specify the number and types of arguments. Whereas in C

double computeArea();

means that there is some function called computeArea that returns double , in C++ it is the same as

double computeArea(void);

That is, it specifies that computeArea takes no arguments.

Presumably the difference is because C++ allows functions to be overloaded, whereas C doesn't.

You've declared computeArea to take exactly zero arguments in the prototype (above main). The fact that you've then defined it to take a double down below is beside the point. Inside main, you're calling it with an argument, which, according to the prototype, is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM