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;
}
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.
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 ...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With