This is a bit tricky to describe, so bear with me. I have 2 separate libraries I’m trying to mesh. One library is a numerical optimizer (right now the Nelder Mead optimizer is implemented) and the other is an sensor calibration library. The optimizer library needs the user to specify and pass a pointer to a callback function to “optimize over”. The sensor library has a generic sensor class to store calibration data for various sensors (one class for each sensor) and needs to call this optimizer and pass the callback. However, since the callback function is a non-static member of the sensor class, it throws errors like “error: invalid use of non-static member function”.
Below is a really dumbed down set of files to serve as a basic example of what I’m trying to do:
optimize.h
#pragma once
double Optimizer( double (*func)(const double&),
const double& arg)
{
return func(arg);
}
testLib.h
#pragma once
class Calibrator
{
public:
double testFun(const double& arg);
double doThing();
double x = 9.8;
};
testLib.cpp
#include "testLib.h"
#include "optimize.h"
double Calibrator::testFun(const double& arg)
{
x *= arg;
return x;
}
double Calibrator::doThing()
{
return Optimizer(testFun,
10);
}
How can I fix this situation, preferably without completely restructuring the code by doing something wacky like making the optimizer part of the sensor calibration class?
1