我想通过给类的成员函数作为它的参数来调用外部函数(将函数作为输入)在类内部。我的代码如下:C++调用另一个函数内的类函数
using namespace std;
// This is my external function
double integrate (double a, double b, int steps, double (*func)(double)){
// code for integration using function pointer
};
Class A
{
protected:
vector<double> to_data, from_data;
int data_size;
public:
A(); // constructor
double calculate_parameter(double t){
// some class member function that makes use of other class members
};
double get_result(double x){
double sum = 0;
for (int i=0;i<data_size;i++){
// this is basically what causes the error
sum = sum + integrate(to_data[i],from_data[i],1,calculate_parameter);
}
};
}
但是,它显示我的函数calculate_parameter无法转换的错误。我想出了一个解决这个问题的方法,就是修改外部函数,以便它也接受一个类对象。有没有办法做到这一点,而不实例化一个新的类对象?提前致谢!
谢谢!完美解决问题! – AnotherCodingEnthusiast
@nurettin:“自我满足”?什么??尽量不要这么粗鲁,谢谢。 –
相当精确和最好的答案 –