本文介绍了函数指针生成“无效使用非静态成员函数”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图以更好的方式掌握指针函数概念。所以我有一个非常简单和工作的例子: #include< iostream& using namespace std; int add(int first,int second) { return first + second; } int subtract(int first,int second) { return first-second; } int操作(int first,int second,int(* functocall)(int,int)) { return(* functocall)第二); } int main() { int a,b; int(* plus)(int,int); int(* minus)(int,int); plus =& add; minus =& subtract; a = operation(7,5,add); b = operation(20,a,minus); cout<< a =<< a<< 和b =< b<< endl; return 0; } 到目前为止很好,现在我需要将函数在类中,并根据我使用的函数指针选择加或减。所以我只是做一个小修改为: #include< iostream> using namespace std; class A { public: int add(int first,int second) { return first + second; } int subtract(int first,int second) { return first-second; } int操作(int first,int second,int(* functocall)(int,int)) { return(* functocall)第二); } }; int main() { int a,b; A a_plus,a_minus; int(* plus)(int,int)= A :: add; int(* minus)(int,int)= A :: subtract; a = a_plus.operation(7,5,plus); b = a_minus.operation(20,a,minus); cout<< a =<< a<< 和b =< b<< endl; return 0; } ,显而易见的错误是: ptrFunc.cpp:在函数'int main()': ptrFunc.cpp:87:29:error:invalid使用非静态成员函数' int A :: add(int,int)' ptrFunc.cpp:88:30:error:invalid使用非静态成员函数'int A :: subtract(int,int)' coz我没有指定要调用的对象(我现在不想使用静态方法) p> 编辑: 几个意见和答案建议不可能是非静态版本(如我所写) all)所以,以下面的方式修改类也不起作用: #include< ; iostream> using namespace std; class A { int res; public: A(int choice) { int(* plus)(int,int)= A :: add; int(* minus)(int,int)= A :: subtract; if(choice == 1) res = operation(7,5,plus); if(choice == 2) res = operation(20,2,minus); cout<< operation of operation =< res; } int add(int first,int second) { return first + second; } int subtract(int first,int second) { return first-second; } int操作(int first,int second,int(* functocall)(int,int)) { return(* functocall)第二); } }; int main() { int a,b; A a_plus(1); A a_minus(2); return 0; } 产生此错误: ptrFunc.cpp:在构造函数'A :: A(int)': ptrFunc.cpp:11:30:从类型'int(A ::)(int,int)'到类型'int(*)(int,int)' ptrFunc.cpp:12:31:从类型'int(A ::)(int,int)'到类型'int(*)(int,int)' 我可以知道如何解决这个问题吗? 感谢方案声明一个指向成员方法的函数指针的语法是: int加)(int,int)=& A :: add; int(A :: * minus)(int,int)=& A :: subtract; 要调用成员方法,请使用*或 - > *运算符: (a_plus。* plus)(7,5); 另请参阅 http://msdn.microsoft.com/en-us/library/b0x1aatf(v = vs.80).aspx 希望这有助。 完成代码: $ b b #include< iostream> using namespace std; class A { public: int add(int first,int second) { return first + second; } int subtract(int first,int second) { return first-second; } int操作(int first,int second,int(A :: * functocall)(int,int)) { return(this-> ; * functocall)(first,second); } }; int main() { int a,b; A a_plus,a_minus; int(A :: * plus)(int,int)=& A :: add; int(A :: * minus)(int,int)=& A :: subtract; a = a_plus.operation(7,5,plus); b = a_minus.operation(20,a,minus); cout<< a =<< a<< 和b =< b<< endl; return 0; } I am trying to grasp pointer function concept in a better way. So I have a very simple and working example as:#include <iostream>using namespace std;int add(int first, int second){ return first + second;}int subtract(int first, int second){ return first - second;}int operation(int first, int second, int (*functocall)(int, int)){ return (*functocall)(first, second);}int main(){ int a, b; int (*plus)(int, int); int (*minus)(int, int); plus = &add; minus = &subtract; a = operation(7, 5, add); b = operation(20, a, minus); cout << "a = " << a << " and b = " << b << endl; return 0;}So far so good,Now I need to group the functions in a class, and select add or subtract based on the function pointer that i use. So I just make a small modification as:#include <iostream>using namespace std;class A{public:int add(int first, int second){ return first + second;}int subtract(int first, int second){ return first - second;}int operation(int first, int second, int (*functocall)(int, int)){ return (*functocall)(first, second);}};int main(){ int a, b; A a_plus, a_minus; int (*plus)(int, int) = A::add; int (*minus)(int, int) = A::subtract; a = a_plus.operation(7, 5, plus); b = a_minus.operation(20, a, minus); cout << "a = " << a << " and b = " << b << endl; return 0;}and the obvious error is:ptrFunc.cpp: In function ‘int main()’:ptrFunc.cpp:87:29: error: invalid use of non-static member function ‘int A::add(int, int)’ptrFunc.cpp:88:30: error: invalid use of non-static member function ‘int A::subtract(int, int)’coz I haven't specified which object to invoke(and I don't want to use static methods for now)EDIT:several comments and answers suggested that the non-static version(as I have written) is not possible.(thanks to all)So,Modifying the class in the following manner also wont work:#include <iostream>using namespace std;class A{ int res;public: A(int choice) { int (*plus)(int, int) = A::add; int (*minus)(int, int) = A::subtract; if(choice == 1) res = operation(7, 5, plus); if(choice == 2) res = operation(20, 2, minus); cout << "result of operation = " << res; }int add(int first, int second){ return first + second;}int subtract(int first, int second){ return first - second;}int operation(int first, int second, int (*functocall)(int, int)){ return (*functocall)(first, second);}};int main(){ int a, b; A a_plus(1); A a_minus(2); return 0;}generated this error:ptrFunc.cpp: In constructor ‘A::A(int)’:ptrFunc.cpp:11:30: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’ptrFunc.cpp:12:31: error: cannot convert ‘A::subtract’ from type ‘int (A::)(int, int)’ to type ‘int (*)(int, int)’may I know how to solve this issue please?thanks 解决方案 The syntax to declare a function pointer to member methods is:int (A::*plus)(int, int) = &A::add;int (A::*minus)(int, int) = &A::subtract;To invoke member methods use .* or ->* operator: (a_plus.*plus)(7, 5);Also have a look at http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspxHope this helps.Complete code: #include <iostream> using namespace std; class A { public: int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int (A::*functocall)(int, int)) { return (this->*functocall)(first, second); } }; int main() { int a, b; A a_plus, a_minus; int (A::*plus)(int, int) = &A::add; int (A::*minus)(int, int) = &A::subtract; a = a_plus.operation(7, 5, plus); b = a_minus.operation(20, a, minus); cout << "a = " << a << " and b = " << b << endl; return 0; } 这篇关于函数指针生成“无效使用非静态成员函数”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 12:12