Thread: Problem about use an array of pointer-to-member-function?

  1. #1
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23

    Problem about use an array of pointer-to-member-function?

    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
    */

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Brackets in the macro definition are unbalanced.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    I got it. thanks very much grumpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to member function
    By ritzmax72 in forum C++ Programming
    Replies: 10
    Last Post: 09-03-2011, 11:29 AM
  2. Pointer + Array + Function Problem~~
    By Vinsento in forum C Programming
    Replies: 4
    Last Post: 07-26-2011, 06:49 AM
  3. Member Function Pointer...
    By yaya in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2009, 05:37 PM
  4. Replies: 3
    Last Post: 07-23-2006, 01:09 PM
  5. Pointer to a Member Function
    By TheDan in forum C++ Programming
    Replies: 25
    Last Post: 04-03-2006, 08:18 PM