i writes a test and get trouble, sure. but i don't understand to fix it. please show me why? thank you.
Code:
#include <iostream>
#define CALL_MEMBER_FN(object, ptrToMember) ((object).*ptrToMember))
class one {
public:
int min(int a, int b) { return (a<b)?a:b; }
int max(int a, int b) { return (a>b)?a:b; }
};
typedef int (one::*fp)(int, int);
int main() {
fp farr[] = { &one::min, &one::max };
one oo;
int ans1 = CALL_MEMBER_FN(oo,farr[0])(2,5);
int ans2 = CALL_MEMBER_FN(oo,farr[1])(2,5);
// std::cout << "min(2,5) = " << CALL_MEMBER_FN(oo,farr[0])(2,5) << std::endl;
// std::cout << "max(2,5) = " << CALL_MEMBER_FN(oo,farr[1])(2,5) << std::endl;
return 0;
}
/* output:
arr_fp_c++_0.cc: In function ‘int main()’:
arr_fp_c++_0.cc:12: error: invalid use of non-static member function
arr_fp_c++_0.cc:12: error: expected ‘,’ or ‘;’ before ‘)’ token
arr_fp_c++_0.cc:13: error: invalid use of non-static member function
arr_fp_c++_0.cc:13: error: expected ‘,’ or ‘;’ before ‘)’ token
*/