簡體   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