任务:创建一个类成员函数,其参数既是变量又是另一个类的其他成员函数。
//hpp
class first {
public:
void function (int func(int, int), int, int, int);
void control (void);
};
//cpp
extern first First;
first First;
int f2(int z){
return z*z;
}
void first::function (int f(int, int), int a, int b, int z){
test_displ.sl_x1 = f(a, b); // вывод на дисплей значения функции
test_displ.sl_x2 = f2(z); // вывод на дисплей значения функции f2
}
int f1(int x, int y){
return x+y;
}
void first::control(void){ // 10Hz cycle
inter.function(f1, 1, 2, 3);
} // работающий код
在这种形式下,一切正常,值显示在屏幕上。我需要该函数f1
成为该类的成员函数second
。当编写下面的代码时,出现错误A pointer to a bound function may only be used to call the function
。据我了解,问题在于函数 f1 不是自由函数,而是成员函数。问题是,我该如何更改它才能使下面的代码起作用?
//hpp
class first {
public:
void function (int func(int, int), int, int, int);
void control (void);
};
class second {
public:
void f1 (int, int);
}
//cpp
extern first First;
extern second Second;
first First;
second Second;
int f2(int z){
return z*z;
}
void first::function (int f(int, int), int a, int b, int z){
test_displ.sl_x1 = f(a, b); // вывод на дисплей значения функции
test_displ.sl_x2 = f2(z); // вывод на дисплей значения функции f2
}
int second::f1(int x, int y){
return x+y;
}
void first::control(void){ // 10Hz cycle
inter.function(Second.f1, 1, 2, 3);
} // не работающий код
一切都令人困惑。您的声明中有某些类型的函数,而实现中有不同类型的函数......
也许您想要类似的东西?