C++ Calling a function from the header file in the main
Your problem is, square
is a member function AKA method. In other words, there is no plain symbol square
anywhere, it's functions::square
. And because it is not a static method, it makes no sense alone, it always needs a corresponding object instance, which you do not have and which IntegralCalculator
couldn't use even if you did.
3 solutions, in the order of simplest, closest to your question, best:
From the constructor delcaration, move the square
out of the class, make it a global function:
In functions.h outside any class:
double square(double aX);
In functions.cpp
double square(double aX) {
return aX*aX;
}
Usage in main.cpp:
IntegralCalculator Function(square);
Or make it static method (which is how you would have to do if you used a language like Java, which didn't allow "naked" functions, but is not really C++ way):
in functions.h:
class functions // inheriting IntegralCalculator removed as not needed
{
public:
static double square(double aX);
};
and in functions.cpp:
//static
double functions::square(double aX) {
return aX*aX;
}
Then you can pass it like this to `Function constructor in main.cpp:
IntegralCalculator Function(functions::square);
Finally, perhaps the best solution, sort of combining the best of the two above: put square
in a namespace. So functions.h becomes
namespace functions {
double square(double aX)
}
functions.cpp becomes
namespace functions {
double square(double aX) {
return aX*aX;
}
}
You still use it in main like this:
IntegralCalculator Function(functions::square);
Cmertin
Updated on February 16, 2020Comments
-
Cmertin about 4 years
I'm writing an integral calculator. I'm trying to get the main.cpp file to read in the functions from the functions class which is in the functions.h.cpp files. I have a function defined as the following for the easy case:
double square(double aX) { return aX*aX; }
This function works when I include it in the main file, though it doesn't want to work when I try and call it from the main file when it's in the functions file. Here is the code that I have:
Main.cpp
file:#include <iostream> #include "IntegralCalculator.h" #include "functions.h" using namespace std; int main() { IntegralCalculator Function(square); Function.print(); }
As you can see, there is another class called
IntegralCalculator
which actually does the calculation of the integral, though nothing should be wrong with that as it works when the function definition is included in the main, and the function is passed as a parameter. Thefunctions.h
file is defined as the following:#ifndef FUNCTIONS_H #define FUNCTIONS_H #include <iostream> #include <cmath> #include "IntegralCalculator.h" class functions : public IntegralCalculator { public: functions(); double square(double aX); }; #endif
and the
functions.cpp
file is defined as such:#include <iostream> #include <cmath> #include "functions.h" double functions::square(double aX) { return aX*aX; }
I know that as of right now I don't need the
<cmath>
library, though I'm going to need it for more complicated functions when I add them.I have tried inheriting the functions class in the main, and I have tried creating a namespace for the functions class and defining the function, however none of these have worked. When I go to compile, I get the following error:
Main.cpp: In function ‘int main()’:
Main.cpp:9: error: ‘square’ was not declared in this scope
If someone could please help me figure out this problem. This code that I'm trying to write is more advanced than my class has gone over, so I'm not sure how to fix it, though I want to write the program this way.
Edit: In case you were curious, I am including the
functions.h
andfunctions.cpp
files in the Makefile. I was getting the same error when I was trying to put thesquare(double aX)
definition in theIntegralCalculator
class as well.