【问题标题】:Compile Error: Function does not take 1 arguments编译错误:函数不接受 1 个参数
【发布时间】:2014-02-26 00:54:50
【问题描述】:
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 computeArea();

标签: c++ visual-c++ compiler-errors


【解决方案1】:
double computeArea();
double area = computeArea(size);
double computeArea(int size) {

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

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

double computeArea(int);

当然,其他原型也是如此。

【讨论】:

  • 非常感谢!我不知道您需要在原型中说明参数的类型。
  • @Jesse,在 C 语言中,你不知道。将它们留在那里意味着有 indeterminate 数量的任何类型的参数。在 C++ 中,将它们排除在外与使用 void 相同 - 恰好有 个参数。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    double computeArea();
    

    表示有一个名为computeArea的函数返回double,在C++中与此相同

    double computeArea(void);
    

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

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

    【讨论】:

      【解决方案4】:

      您已声明 computeArea 在原型(在 main 上方)中采用零参数。您随后将其定义为在下面进行双倍下降的事实是无关紧要的。在 main 内部,您使用参数调用它,根据原型,这是错误的。

      【讨论】:

        猜你喜欢
        • 2013-03-26
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 2016-04-02
        • 1970-01-01
        • 1970-01-01
        • 2011-07-07
        • 1970-01-01
        相关资源
        最近更新 更多