简体   繁体   English

“不能用作函数错误”

[英]“cannot be used as a function error”

I am writing a simple program that uses functions found in different .cpp files. 我正在编写一个使用不同.cpp文件中的函数的简单程序。 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" . 我得到的错误是“'functionname'不能用作函数” The function it says cannot be used is the growthRate function and the estimatedPopulation function. 它说不能使用的函数是growthRate函数和estimatedPopulation函数。 The data comes in through an input function (which I do think is working). 数据通过输入函数(我认为它正在工作)进入。

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: growthRate函数:

 #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;
}    

You are using growthRate both as a variable name and a function name. 您将growRate用作变量名和函数名。 The variable hides the function, and then you are trying to use the variable as if it was the function - that is not valid. 该变量隐藏了该函数,然后您尝试使用该变量,就好像它是函数一样 - 这是无效的。

Rename the local variable. 重命名局部变量。

#include "header.h"

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

Your compiler is right. 你的编译器是对的。 You can't use the growthRate variable you declared in main as a function. 您不能将在main中声明的growRate变量用作函数。

Maybe you should pick different names for your variables so they don't override function names? 也许您应该为变量选择不同的名称,以便它们不会覆盖函数名称?

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 . 这里可能更重要 - C ++(和C)名称具有范围 You can have two things named the same but not at the same time. 你可以有两个相同但不同时命名的东西。 In your particular case your grouthRate variable in the main() hides the function with the same name. 在您的特定情况下, main()中的grouthRate变量会隐藏具有相同名称的函数。 So within main() you can only access grouthRate as float . 所以在main()你只能访问grouthRate作为float On the other hand, outside of the main() you can only access that name as a function, since that automatic variable is only visible within the scope of main() . 另一方面,在main()之外,您只能将该名称作为函数访问,因为该自动变量仅在main()的范围内可见。

Just hope I didn't confuse you further :) 只是希望我没有进一步混淆你:)

You can't pass a function as a parameter. 您不能将函数作为参数传递。 Simply remove it from estimatedPopulation() and replace it with 'float growthRate'. 只需从estimatedPopulation()中删除它,并将其替换为'float growthRate'。 use this in your calculation instead of calling the function: 在计算中使用它而不是调用函数:

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

and call it as: 并称之为:

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

Modify your estimated population function to take a growth argument of type float. 修改估计的填充函数以采用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. 然后,您可以使用birthRate和deathRate调用growthRate函数,并使用返回值作为增长到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));

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

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