Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cannot be used as a function error"

Tags:

c++

function

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 growthRate function and the estimatedPopulation function. 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:

 #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;
}    
like image 956
darko Avatar asked Dec 10 '10 19:12

darko


People also ask

How do I fix this function was not declared in this scope?

To resolve this error, a first method that is helpful would be declaring the function prototype before the main() method. So, we have used the function prototype before the main method in the updated code. When we have compiled the code, it throws no exceptions and runs properly.

What is not declared in scope?

As from the name we can understand that when the compiler of Arduino IDE is unable to recognize any variable or is unable to process any loop or any instruction having any undeclared variable so it gives the error “not declared in this scope”, which means that code is unable to understand the instruction given in the ...


1 Answers

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.

like image 67
Martin v. Löwis Avatar answered Sep 19 '22 12:09

Martin v. Löwis