“cannot be used as a function error”

后端 未结 6 1170
孤城傲影
孤城傲影 2021-02-19 01:45

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 fun

相关标签:
6条回答
  • 2021-02-19 01:48

    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:

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

    and call it as:

    int foo = estimatedPopulation (currentPopulation, growthRate (birthRate, deathRate));
    
    0 讨论(0)
  • 2021-02-19 01:49

    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.

    Rename the local variable.

    0 讨论(0)
  • 2021-02-19 01:56
    #include "header.h"
    
    int estimatedPopulation (int currentPopulation, float growthRate)
    {
        return currentPopulation + currentPopulation * growthRate  / 100;
    }
    
    0 讨论(0)
  • 2021-02-19 02:03

    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 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));
    
    0 讨论(0)
  • 2021-02-19 02:07

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

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

    0 讨论(0)
  • 2021-02-19 02:08

    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 grouthRate variable in the main() hides the function with the same name. So within main() you can only access grouthRate as 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().

    Just hope I didn't confuse you further :)

    0 讨论(0)
提交回复
热议问题