在非靜態類中,我可以直接傳遞如下的函數成員rhs嗎?它報告了一些錯誤。我也試圖將其更改爲mystepper.do_step(this->rhs)
它仍然不起作用。但如果我把mystepper.do_step(rhs);
放在主函數中,而rhs作爲一個函數,它就可以正常工作。我該如何解決這個問題?非常感謝!傳遞函數成員作爲類C++中的參數
void Animal::rhs(const double x , double &dxdt , const double t) {
dxdt = 2*t;
};
void Animal::response() {
mystepper.do_step(rhs);
}
我做了一些最低限度的代碼來說明我以前question.Your幫助表示高度讚賞!
#include <iostream>
using namespace std;
class ABC{
private:
int x =3;
int add2num(int a, int b){
return a+b+x;
}
int worker(int &fun(int a, int b), int a, int b){
return fun(a,b);
}
public:
int doSomething(int a, int b){
return worker(add2num, a, b);
}
};
int main() {
ABC test;
cout << test.doSomething(3,5) << endl;
return 0;
}
使它成爲'靜態'。 – StoryTeller
使它成爲'靜態函數'或使用'std :: bind'''lambda函數'來知道它應該被調用的對象。 – paweldac
它通過改變rhs作爲靜態函數來工作!但是,如果我在動物發起的不同實例中有不同的rhs中有多個參數,那該怎麼辦呢?任何其他方式使代碼工作,而不改變rhs作爲靜態函數? – drbombe