Results 1 to 2 of 2

Thread: pass member function as argument int template function

  1. #1
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default pass member function as argument int template function

    Hi everyone

    I am trying to pass a member function as an argument to another template member funtion.
    But during build time i am getting an error -: error: no matching function for call to 'A::do_op(int&, int&, <unresolved overloaded function type>)'

    My .h file is :

    Qt Code:
    1. class A
    2. {
    3. public:
    4. A();
    5.  
    6. int test_op(int x,int y);
    7.  
    8. private:
    9. int add(int x, int y);
    10. int sub(int x, int y);
    11.  
    12. template<class F>
    13. int do_op(int x, int y, F &fun)
    14. {
    15. return fun(x,y);
    16. }
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    cpp file :

    Qt Code:
    1. template<class F>
    2. int do_op_global(int x, int y, F &fun)
    3. {
    4. return fun(x,y);
    5. }
    6. int add_global(int x, int y)
    7. {
    8. return (x+y);
    9. }
    10.  
    11.  
    12. A::A()
    13. {}
    14.  
    15. int A::add(int x, int y)
    16. {
    17. return (x+y);
    18. }
    19.  
    20. int A::sub(int x, int y)
    21. {
    22. return (x-y);
    23. }
    24.  
    25. int A::test_op(int x, int y)
    26. {
    27. int m = do_op_global(x,y,add_global); // Statement 1
    28. int n = do_op(x,y,sub); // Statement 2
    29.  
    30. return (m+n);
    31. }
    To copy to clipboard, switch view to plain text mode 

    Only statement 2 is giving error.

    Is there any restriction for template function inside class ?
    Please help me how i will pass a member function as argument.

    thanks.

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pass member function as argument int template function

    Use std::mem_fun (http://www.cplusplus.com/reference/functional/mem_fun/) it wraps member function inside function object.

Similar Threads

  1. QFile as an argument of a function?
    By Stanfillirenfro in forum Qt Programming
    Replies: 18
    Last Post: 2nd February 2013, 18:15
  2. How to pass an argument to evaluateJavaScript function?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 5
    Last Post: 5th January 2013, 10:45
  3. Function: ... Argument
    By sonulohani in forum General Programming
    Replies: 3
    Last Post: 15th December 2012, 16:55
  4. Replies: 2
    Last Post: 28th March 2012, 22:47
  5. Replies: 2
    Last Post: 23rd February 2012, 13:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.