Results 1 to 5 of 5

Thread: Help implement an array of member functions ( member function pointers)?

  1. #1
    Join Date
    May 2018
    Posts
    13
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Help implement an array of member functions ( member function pointers)?

    I'm working a on a program and what it does when the mouse is dragged or clicked will depend on what mode it is in.

    This is a simplified version of how I would like it to work, but this is giving me all sorts of errors, starting with errors in cstdlib and into cmath.

    Is there a way to do this?

    .pro :
    Qt Code:
    1. TEMPLATE = app
    2. CONFIG += console
    3. CONFIG -= app_bundle
    4. CONFIG -= qt
    5.  
    6. SOURCES += \
    7. main.c
    To copy to clipboard, switch view to plain text mode 

    main.c
    Qt Code:
    1. #include <stdio.h>
    2. #include<iostream>
    3.  
    4. using namespace std;
    5.  
    6.  
    7. class MyClass{
    8. public:
    9. static const int SCREAM = 0,WHISPER = 1,SLEEP = 2;
    10. void emote();
    11.  
    12. private:
    13. typedef void(MyClass::*reaction)();
    14. reaction react[3]= {&MyClass::scream,&MyClass::whisper,&MyClass::sleep};
    15. void scream();
    16. void whisper();
    17. void sleep();
    18.  
    19. };
    20.  
    21. void MyClass::scream(){
    22. cout << "eeeeek"<< endl;
    23. }
    24. void MyClass::whisper(){
    25. cout << "ssshhhhhhh"<< endl;
    26. }
    27. void MyClass::sleep(){
    28. cout << "zzzzZZZZZ"<< endl;
    29. }
    30. void MyClass::emote(){
    31. react[SCREAM]();
    32. react[WHISPER]();
    33. react[SLEEP]();
    34. }
    35. int main()
    36. {
    37. MyClass c;
    38. c.emote();
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help implement an array of member functions ( member function pointers)?

    There is nothing in the code you posted that would cause errors arising in cstdlib or cmath. You don't even use anything from cmath in the code posted. You've likely "simplified" it a little too much.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    May 2018
    Posts
    13
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help implement an array of member functions ( member function pointers)?

    I started a new project and pasted the above code into the .pro file and main.cpp file

    I now get these errors:

    Y:\QTProjects\TestFunctionPtrArray\main.cpp:31: error: must use '.*' or '->*' to call pointer-to-member function in '((MyClass*)this)->MyClass::react[0] (...)', e.g. '(... ->* ((MyClass*)this)->MyClass::react[0]) (...)'
    react[SCREAM]();
    Y:\QTProjects\TestFunctionPtrArray\main.cpp:32: error: must use '.*' or '->*' to call pointer-to-member function in '((MyClass*)this)->MyClass::react[1] (...)', e.g. '(... ->* ((MyClass*)this)->MyClass::react[1]) (...)'
    react[WHISPER]();
    Y:\QTProjects\TestFunctionPtrArray\main.cpp:33: error: must use '.*' or '->*' to call pointer-to-member function in '((MyClass*)this)->MyClass::react[2] (...)', e.g. '(... ->* ((MyClass*)this)->MyClass::react[2]) (...)'
    react[SLEEP]();


    Added after 13 minutes:


    I posted on another c++ forum and they suggested I make the functions static and change the typedef to

    Qt Code:
    1. private:
    2. typedef void(*reaction)();
    3. reaction react[3]= {&MyClass::scream,&MyClass::whisper,&MyClass::sleep};
    4. static void scream();
    5. static void whisper();
    6. static void sleep();
    7.  
    8. };
    To copy to clipboard, switch view to plain text mode 

    Doing this and it works as expected.
    Last edited by shawnlau; 4th July 2018 at 13:07.

  4. #4
    Join Date
    May 2018
    Posts
    13
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help implement an array of member functions ( member function pointers)?

    Quote Originally Posted by d_stranz View Post
    There is nothing in the code you posted that would cause errors arising in cstdlib or cmath. You don't even use anything from cmath in the code posted. You've likely "simplified" it a little too much.
    I see what happened that created all those weird errors. When I started the project, I started it a non qt C project. It should have been a C++ project. See the :
    Qt Code:
    1. #include <stdio.h>
    2. #include<iostream>
    To copy to clipboard, switch view to plain text mode 


    The MSVC2017 kit didn't point out this error. and the errors I was getting were like this:
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\in clude\cstdlib:19: error: C2061: syntax error: identifier 'noexcept'
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\in clude\cstdlib:19: error: C2059: syntax error: ';'
    etc.

    When I deleted the build folder and reconfigured the project to use the MinGW 32bit kit, it pointed out right away it could not find the <iostream>. Thats when I noticed the mistake.

  5. #5
    Join Date
    May 2018
    Posts
    13
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Help implement an array of member functions ( member function pointers)?

    I ran into some problems making functions static in my real program as they were trying to call non static functions.

    So in the the simplified version, this way allows me to make the array and keep the functions non-static;

    Qt Code:
    1. #include<iostream>
    2. #include <functional>
    3. #include <string>
    4. using namespace std;
    5.  
    6.  
    7. class MyClass{
    8. public:
    9. static const int SCREAM = 0,WHISPER = 1,SLEEP = 2;
    10. void emote();
    11.  
    12. private:
    13.  
    14. std::function<bool(MyClass*,string) > react[3]= {&MyClass::scream,&MyClass::whisper,&MyClass::sleep};
    15. bool scream(string s);
    16. bool whisper(string s);
    17. bool sleep(string s);
    18.  
    19. };
    20.  
    21. bool MyClass::scream(string s){
    22. cout << s << endl;
    23. return true;
    24. }
    25. bool MyClass::whisper(string s){
    26. cout << s << endl;
    27. return false;
    28. }
    29. bool MyClass::sleep(string s){
    30. cout << s << endl;
    31. return true;
    32. }
    33. void MyClass::emote(){
    34. bool a,b,c;
    35. a =react[SCREAM](this,"eeek");
    36. b = react[WHISPER](this,"shhh");
    37. c = react[SLEEP](this, "zzzz");
    38. cout<< a << " " << b << " "<< c<<endl;
    39. }
    40. int main()
    41. {
    42. MyClass c;
    43. c.emote();
    44.  
    45. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 26th October 2013, 05:40
  2. Replies: 2
    Last Post: 28th March 2012, 21:47
  3. Replies: 2
    Last Post: 23rd February 2012, 12:23
  4. Replies: 22
    Last Post: 8th October 2008, 13:54
  5. Pointers to Member Functions
    By Doug Broadwell in forum General Programming
    Replies: 8
    Last Post: 15th May 2007, 23:08

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.