I want to keep my code modular. At the moment I have my code set-up to pass functions from a child class to a parent class. However it does not compile at all. Now I want to get rid of the passing functions all together but keep the 'modular-ness'.
Updates: I added more information about what my code is doing. I still left out majority of what I am doing. Class Molecule is optimizing multiple instances of Class Rates. Class Rates is optimizing multiple values that are generated by a single function inside Rates.
Class Data_Analysis {
virtual double find_rms_A (vector<double>) = 0;
virtual double find_rms_B (vector<double>) = 0;
virtual double find_rms_C (vector<double>) = 0;
double E (double (Data_Analysis::*fxn(vector<double>)) {
// doing tons of stuff
(this->*fxn)(vec);
//Simplex is third party library that requires a function that
// takes vector<double> and outputs a double
//http://www.codeguru.com/cpp/article.php/c17505/Simplex-Optimization-Algorithm-and-Implemetation-in-C-Programming.htm
Simplex((this->*fxn)(vec));
}
};
Class Molecule: Data_Analysis {
virtual double find_rms_A (vector<double> ) {
// using variables only declared in Molecule
double rms = 0.0
for ( int data_point_A = 0; data_point_A < num_data_point_A; data_point_A++) {
Rates r(data_point_A);
r.run_simulation_v1();
rms += r.return_rate();
}
return rms;
}
virtual double find_rms_B (vector<double>) {
// using variables only declared in Molecule
double rms = 0.0
for ( int data_point_B = 0; data_point_B < num_data_point_B; data_point_B++) {
//do stuff
rms += rate;
}
return rms;
}
void optimize_A () {
// set variables for type of optimization A
E(&Data_Analysis::find_rms_A);
}
void optimize_B () {
// // set variables for type of optimization B
E(&Data_Analysis::find_rms_B);
}
};
Class Rates: Data_Analysis {
virtual double find_rms_C (vector<double>) {
// using variables only declared in Rates
double rms = 0.0
for ( int data_point_C = 0; data_point_C < num_data_point_C; data_point_C++) {
// run simulation that is completely different than anything used in Molecule
rms += rate;
}
return rms;
}
void optimize_C () {
// set variables for type of optimization C
E(&Data_Analysis::find_rms_C);
}
};
Things I have tried to make passing functions work:
Virtual Function 1, Virtual Function 2, Virtual Function 3: "cannot declare variable ‘r’ to be of abstract type ‘Child2’"
Pointer Functions 1, Pointer Functions 2: "cannot convert ‘double (Child1::)(std::vector)’ to ‘Parent::fxn {aka double ()(std::vector)}’ in initialization" (The asterisks are making things italics.)
So, I want to re-organize my code to get around passing functions. But I have no idea how to do this without getting rid of 'function E' and repeating the code in functions A-D (aka destroying the modular-ness). Any tips/advice?