Results 1 to 8 of 8

Thread: Cannot write a function pointer into a class implementation

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Cannot write a function pointer into a class implementation

    I have a MainWindow class with ther .h and cpp files.
    Compiler tells me :

    argument of type 'void (MainWindow:(long int)' does not match 'void (*)(long int)'

    I have tried all combinations, with class prefix, without it,.... without success.

    Qt Code:
    1. void MainWindow::update1(long value){ }
    2.  
    3. void MainWindow::on_pushButton_6_clicked()
    4. {
    5. void (*pFunc)(long);
    6. pFunc = update1; // here is where I have the error.
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 


    Any help ? Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot write a function pointer into a class implementation

    You are declaring a C function pointer but attempting to assign a C++ function. This is not possible. You must define the class instance as well.

    eg. void(MainWindow::*pFunc)(long)

  3. The following user says thank you to squidge for this useful post:

    tonnot (18th April 2011)

  4. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot write a function pointer into a class implementation

    Ok, so simple....
    Thanks


    Added after 1 15 minutes:


    And how can I write it to pass to a function of another class ??
    How I can define this class_B function to receive pointers of functions of any parent class ?
    Last edited by tonnot; 18th April 2011 at 18:59.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot write a function pointer into a class implementation

    Quote Originally Posted by tonnot View Post
    And how can I write it to pass to a function of another class ??
    You change "MainWindow" in the pointer declaration to the class of your choice.

    How I can define this class_B function to receive pointers of functions of any parent class ?
    You use the parent class name in the pointer declaration.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot write a function pointer into a class implementation

    So, I cannot write a generic class body to accept function pointers of any class ?
    Thanks

  7. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot write a function pointer into a class implementation

    The compiler needs to know the class otherwise it doesn't know how to access the class member variables (the 'this' keyword wouldn't work)

    However, you can create a standard function, use a C-style function pointer and then make it a friend of the relevent class. You will however have to pass a pointer to the class instance if you require to access any class member functions or variables as a standard C function pointer will not populate 'this'.

  8. The following user says thank you to squidge for this useful post:

    tonnot (18th April 2011)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Cannot write a function pointer into a class implementation

    Quote Originally Posted by tonnot View Post
    So, I cannot write a generic class body to accept function pointers of any class ?
    Thanks
    Usually you do it the other way round. You expose a generic interface (using virtual methods) which is then implemented by some class so that another class/object can call methods of the interface. Just like Designer handles custom widgets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    tonnot (18th April 2011)

  11. #8
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Cannot write a function pointer into a class implementation

    Ok, thank you very much

Similar Threads

  1. Function for repositioning the file pointer
    By psantofe in forum Qt Programming
    Replies: 7
    Last Post: 9th September 2010, 13:36
  2. Replies: 7
    Last Post: 2nd September 2010, 19:42
  3. Replies: 14
    Last Post: 1st December 2009, 20:45
  4. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 15:59
  5. Replies: 3
    Last Post: 16th May 2007, 11:07

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
  •  
Qt is a trademark of The Qt Company.