namespace MyNPace { class ClassA1B { public: ClassA1B(void (*djfu)(int)); void toRun(void); private: void (*PF)(int); }; class CallA1B { public: void CCAB(int); void HUI(void); }; } MyNPace::ClassA1B::ClassA1B(void (*f2u)(int)) { PF = f2u; } void MyNPace::ClassA1B::toRun() { PF(7); } void MyNPace::CallA1B::CCAB(int q) { q = 9; } void MyNPace::CallA1B::HUI(void) { ClassA1B* ca = new ClassA1B(CCAB); }
ClassA1B* ca = new ClassA1B(CCAB);
报错:
no instance of constructor "MyNPace::ClassA1B::ClassA1B" matches the argument list
如何修改?
将void CCAB(int);
改为static void CCAB(int);
你用的啥编译器,报错咋那么不精确?
这个是我的报错:
test.cpp: In member function ‘void MyNPace::CallA1B::HUI()’:
test.cpp:25:33: error: invalid use of non-static member function ‘void MyNPace::CallA1B::CCAB(int)’
25 | ClassA1B* ca = new ClassA1B(CCAB);
| ^~~~
test.cpp:21:6: note: declared here
21 | void MyNPace::CallA1B::CCAB(int q) {
| ^~~~~~~
OK,懂了。是不是实列方法都不能这样用,因为执行的时候实列方法的对象可能已经没了。
而且语法上也无法满足为函数指针指向的实列方法指定对象