C++ “不能用作函数错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4412619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:19:37  来源:igfitidea点击:

"cannot be used as a function error"

c++function

提问by darko

I am writing a simple program that uses functions found in different .cpp files. All of my prototypes are contained in a header file. I pass some of the functions into other functions and am not sure if I am doing it correctly. The error I get is "'functionname' cannot be used as a function". The function it says cannot be used is the growthRatefunction and the estimatedPopulationfunction. The data comes in through an input function (which I do think is working).

我正在编写一个简单的程序,它使用在不同 .cpp 文件中找到的函数。我所有的原型都包含在一个头文件中。我将一些函数传递给其他函数,但不确定我是否正确执行。我得到的错误是“'functionname' 不能用作函数”。它说不能使用的growthRate功能是功能和estimatedPopulation功能。数据通过输入函数输入(我认为这是有效的)。

Thanks!

谢谢!

header file:

头文件:

#ifndef header_h
#define header_h

#include <iostream>
#include <iomanip>
#include <cstdlib>


using namespace std;

//prototypes
void extern input(int&, float&, float&, int&);
float extern growthRate (float, float);
int extern estimatedPopulation (int, float);
void extern output (int);
void extern myLabel(const char *, const char *);

#endif

growthRate function:

增长率函数:

 #include "header.h"

float growthRate (float birthRate, float deathRate, float growthrt)     
{    
    growthrt = ((birthRate) - (deathRate))
    return growthrt;   
}

estimatedPopulation function:

估计人口函数:

    #include "header.h"

int estimatedPopulation (int currentPopulation, float growthrt)
{
    return ((currentPopulation) + (currentPopulation) * (growthrt / 100);
}

main:

主要的:

#include "header.h"

int main ()
{
    float birthRate, deathRate, growthRate;
    char response; 
    int currentPopulation, years, estimatedPopulation;

    do //main loop
    {  
        input (currentPopulation, birthRate, deathRate, years);
        growthRate (birthRate, deathRate, growthrt);

        estimatedPopulation (currentPopulation, growthrt);
        output (estimatedPopulation (currentPopulation, growthrt));
        cout << "\n Would you like another population estimation? (y,n) ";
        cin >> response;
    }          
    while (response == 'Y' || response == 'y');

    myLabel ("5-19", "12/09/2010");   

    system ("Pause");

    return 0;
}    

回答by Martin v. L?wis

You are using growthRate both as a variable name and a function name. The variable hides the function, and then you are trying to use the variable as if it was the function - that is not valid.

您同时使用 growthRate 作为变量名和函数名。变量隐藏了函数,然后您试图将变量当作函数来使用 - 这是无效的。

Rename the local variable.

重命名局部变量。

回答by khachik

#include "header.h"

int estimatedPopulation (int currentPopulation, float growthRate)
{
    return currentPopulation + currentPopulation * growthRate  / 100;
}

回答by BlackBear

You can't pass a function as a parameter. Simply remove it from estimatedPopulation() and replace it with 'float growthRate'. use this in your calculation instead of calling the function:

您不能将函数作为参数传递。只需将它从estimatedPopulation() 中删除并用'float growthRate' 替换它。在计算中使用它而不是调用函数:

int estimatedPopulation (int currentPopulation, float growthRate)
{
    return (currentPopulation + currentPopulation * growthRate / 100);
}

and call it as:

并将其称为:

int foo = estimatedPopulation (currentPopulation, growthRate (birthRate, deathRate));

回答by Zac Howland

Modify your estimated population function to take a growth argument of type float. Then you can call the growthRate function with your birthRate and deathRate and use the return value as the input for grown into estimatedPopulation.

修改您的估计人口函数以采用 float 类型的增长参数。然后您可以使用您的birthRate 和deathRate 调用growthRate 函数,并将返回值用作growth 到estimatedPopulation 的输入。

float growthRate (float birthRate, float deathRate)     
{    
    return ((birthRate) - (deathRate));    
}

int estimatedPopulation (int currentPopulation, float growth)
{
    return ((currentPopulation) + (currentPopulation) * (growth / 100);
}

// main.cpp
int currentPopulation = 100;
int births = 50;
int deaths = 25;
int population = estimatedPopulation(currentPopulation, growthRate(births, deaths));

回答by Edward Strange

Your compiler is right. You can't use the growthRate variable you declared in main as a function.

你的编译器是对的。您不能将在 main 中声明的 growthRate 变量用作函数。

Maybe you should pick different names for your variables so they don't override function names?

也许您应该为变量选择不同的名称,这样它们就不会覆盖函数名称?

回答by Nikolai Fetissov

This line is the problem:

这一行是问题:

int estimatedPopulation (int currentPopulation,
                         float growthRate (birthRate, deathRate))

Make it:

做了:

int estimatedPopulation (int currentPopulation, float birthRate, float deathRate)

instead and invoke the function with three arguments like

而是使用三个参数调用该函数,例如

estimatePopulation( currentPopulation, birthRate, deathRate );

OR declare it with two arguments like:

或用两个参数声明它,例如:

int estimatedPopulation (int currentPopulation, float growthrt ) { ... }

and call it as

并将其称为

estimatedPopulation( currentPopulation, growthRate (birthRate, deathRate));

Edit:

编辑:

Probably more important here - C++ (and C) names have scope. You can have two things named the same but not at the same time. In your particular case your grouthRatevariable in the main()hidesthe function with the same name. So within main()you can only access grouthRateas float. On the other hand, outside of the main()you can only access that name as a function, since that automaticvariable is only visible within the scope of main().

在这里可能更重要 - C++(和 C)名称具有scope。您可以有两个名称相同但不能同时命名的事物。在您的特定情况下,您的grouthRate变量main()隐藏了具有相同名称的函数。因此,main()您只能grouthRatefloat. 另一方面,在 之外,main()您只能作为函数访问该名称,因为该自动变量仅在main().

Just hope I didn't confuse you further :)

只是希望我没有进一步混淆你:)