无法调用函数

时间:2015-10-08 00:59:58

标签: c++ function arguments

我正在编写处理调用函数的代码。我快要走了一半,当我尝试编译时,我得到了错误"太多的函数参数' double()。' 这是我的代码。任何反馈都会有所帮助。

#include <iostream>
#include <cstdlib>

using namespace std;

void displayMenu();
double force();

int main(int argc, char** argv) 
{
    int menuOption;
    displayMenu();
    system("PAUSE");
    return 0;
}

void displayMenu(void)
{
    int menuOption;
    double weight;
    cout << "           Main Menu" << endl;
    cout << "Enter 1 for Force calculation with acceleration = 9.8m/s^2.\n";
    cout << "Enter 2 for Force calculation with user defined acceleration.\n";
    cout << "Enter 3 to quit the program.\n";
    cin >> menuOption;
        if(menuOption==1)
            cout << "Enter a mass.\n";
            cin >> weight;
            cout << force(weight);  
}

double force(double weight)
{
    double force, mass;
    force=(mass*9.8);
    return force;
}

2 个答案:

答案 0 :(得分:1)

double force();

在顶部告诉编译器force()期望没有args

然后你调用它传递一个arg:

cout << force(weight); 

编译器说&#34;呵呵?&#34;。

修复是让编译器知道force接受参数。在顶部将double force();替换为double force(double);

答案 1 :(得分:1)

double force();更改为double force(double);