#include <iostream>
using namespace std;
class Model
{
private:
double a, b;
double (*fPtr)(double);
public:
//Constructor
Model(double lowerLimit, double upperLimit, double (*func)(double))
{
a = lowerLimit;
b = upperLimit;
fPtr = func;
}
// Destructor
~Model() {}
// Member Function Declaration
double foo1(double x);
double foo2(int N);
double Geta() const { return a; }
double Getb() const { return b; }
};
// Member Function Definition
double Model::foo1(double x)
{
return x * x - x + 1;
}
double Model::foo2(int N)
{
double sum = 0.0;
double h = (b - a) / N;
for (int i = 1; i < N + 1; i++)
{
sum = sum + fPtr(a + (i - 1) * h) + fPtr(a + i * h);
}
return sum * h / 2;
}
int main()
{
Model MyModel(0, 1, foo1);
double result = Model.foo2(100);
cout << "result: " << result << endl;
}
I'm trying to define a class in C++ but I keep getting the error
Model MyModel(0,1,foo1): ‘foo1’ was not declared in this scope.
foo1
is a member function of the class, and the value it returns must be passed in to foo2
. The *fPtr
function pointer must point to foo1
.
I'm unable to understand where foo1
needs to be declared.