繁体   English   中英

编译错误:函数没有1个参数

[英]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;
}

它发生在double area = computeArea(size);的行double area = computeArea(size); 我不明白为什么它说我没有明确地提出自己的意见。

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

这些事情之一与其他事物不一样……

您需要修复原型(第一个)以匹配实际功能:

double computeArea(int);

当然,其他原型也一样。

您搞砸了前向声明,它们还必须反映函数的参数类型。

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

C ++与C的不同之处在于,函数的前向声明必须指定参数的数量和类型。 而在C中

double computeArea();

意味着有一个称为computeArea函数返回double ,在C ++中,它与

double computeArea(void);

也就是说,它指定computeArea接受任何参数。

大概的区别是因为C ++允许函数被重载,而C不允许。

您已经声明computeArea在原型中(在main之上)正好接受零个参数。 然后,您将其定义为在下面加倍,这是事实。 在main内部,您用一个参数调用它,根据原型,这是错误的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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