Results 1 to 9 of 9

Thread: Pointers to Member Functions

  1. #1
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Pointers to Member Functions

    I have a table where one of the items is a pointer to member function, e.g.:

    class State : public QObject {
    QOBJECT
    public:
    State() {}
    ~State() {}
    int Set_State (uint Object, int New_State);
    int Validate_State(int New_State);
    };

    typedef int (State::* pState_Valid)(int Requested_State);

    struct {
    int Current_State;
    pState_Valid pValidator;
    } State_Tbl[] = {
    0, &State::Validate_State,
    1, &State::Validate_State
    };

    int State::Validate_State(int Requested_State) {
    int Validated_State;
    ...
    return(Validated_State);
    }

    int State::Set_State(uint Object, int Requested_State) {
    int Validated_State;

    Validated_State = (*State_Tbl[Object].pValidate_State)(Requested_State);
    ...
    return(Validated_State);
    }

    The error I get (gnu compiler) on the line that's calling the member function is:

    error: invalid use of 'unary*' on pointer to member

    What am I doing wrong?

    Thanks,
    Doug Broadwell

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    Use this instead:
    Validated_State = (State_Tbl[Object].*pValidate_State)(Requested_State);

    You were not dereferencing the function, but the struct object.
    Try dereferencing the function pointer, which is the correct way.

    Regards
    Last edited by wysota; 13th May 2007 at 21:03. Reason: Posts merged

  3. #3
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    Thanks Marcel, that fixed that error. Now I'm getting the error:

    'pValidator' undeclared (first use this function)

    ?????
    Help again.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    on which line in the code this error comes?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    The error is on this line:

    Validated_State = (*State_Tbl[Object].pValidate_State)(Requested_State);

    Doug

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    Quote Originally Posted by Doug Broadwell View Post
    The error is on this line:

    Validated_State = (*State_Tbl[Object].pValidate_State)(Requested_State);

    Doug
    But that is the same code you had before when you started the thread...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    Validated_State = (State_Tbl[Object].*pValidate_State)(Requested_State);

    Who is pValidate_State?
    You should call (State_Tbl[Object].*pValidator)(Requested_State);, because pValidator points to State::Validate_State.

    What is the point of pointers to functions if you will call the functions directly?

  8. #8
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    Oops, you're right, the actual line that is generating the error is the edited one:

    Validated_State = (State_Tbl[Object].*pValidator)(Requested_State);

    My mistake in copying/simplifying the code and having 'pValidate_State' instead of the correct 'pValidator' here. It still gets the 'pValidator undeclared' error.

    Doug

  9. #9
    Join Date
    Jan 2006
    Location
    Berkeley California
    Posts
    109
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pointers to Member Functions

    Just realized one problem, I haven't instantiated any State objects yet to point to!

    My newbe status reveals itself yet again.

    Doug

Similar Threads

  1. Qtopia core 4.2.2 cross compile make error
    By smiyai18 in forum Installation and Deployment
    Replies: 2
    Last Post: 28th August 2007, 17:04
  2. Replies: 2
    Last Post: 16th March 2007, 09:04
  3. array of pointers to functions
    By moowy in forum General Programming
    Replies: 3
    Last Post: 19th October 2006, 10:48
  4. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  5. emiting signals from const member functions !?
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 25th March 2006, 11:29

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.