0

I tried to pass function name as parameter as below:

class RemoteControlMonitor {
private:
    void (*rph)(unsigned int key);
    void (*rrh)(unsigned int key);

public:
    RemoteControlMonitor(void (*pressed)(unsigned int key), 
                     void (*released)(unsigned int key) = 0) {
     *rph = pressed;
     *rrh = released;
     lr_set_handler(remote_control_handler);
    }

    void runPressed() {
     while (!shutdown_requested()) {
         remote_key = 0;
         remote_etype = 0;
         wait_event(&remote_control_pressed, 0);

             if (*rph) {
                 (*rph)(remote_key);
             }
         }
     }
 };

when I compile it, the error is as below, what can I do?

RemoteControlMonitor.H: In method `RemoteControlMonitor::RemoteControlMonitor(void ()(unsigned int), void ()(unsigned int) = 0)':

RemoteControlMonitor.H:61: assignment of read-only location

RemoteControlMonitor.H:61: assignment to void ()(unsigned int)' fromvoid (*)(unsigned int)'

RemoteControlMonitor.H:62: assignment of read-only location

RemoteControlMonitor.H:62: assignment to void ()(unsigned int)' fromvoid (*)(unsigned int)'

1
  • 1
    *rph = pressed => rph = pressed (lose the star). Commented Oct 28, 2013 at 11:56

2 Answers 2

2

try to use typedef it will be more clear.

typedef  void (*keyaction)(unsigned int key);

class RemoteControlMonitor {
private:
    keyaction rph;
    keyaction rrh;

public:
    RemoteControlMonitor(keyaction pressed, 
                     keyaction released = NULL) {
     rph = pressed;
     rrh = released;
     lr_set_handler(remote_control_handler);
    }

    void runPressed() {
     while (!shutdown_requested()) {
         remote_key = 0;
         remote_etype = 0;
         wait_event(&remote_control_pressed, 0);

             if (rph) {
                 (*rph)(remote_key);
             }
         }
     }
 };

EDIT:

this function goes to constructor:

void f(unsigned int){}

you declare it like this:

RemoteControlMonitor  rcm(f);
Sign up to request clarification or add additional context in comments.

4 Comments

RemoteControlMonitor.H:58: invalid type void *' for default argument to void (*)(unsigned int)'
Then I call it with function void my_remote_pressed(unsigned int key) {….} by rcm = new RemoteControlMonitor(&my_remote_pressed); compile error is In function int main(int, char **)': ANSI C++ forbids implicit conversion from void *' in default argument
Follow your code, I did it as below: void my_remote_pressed(unsigned int key) { … } RemoteControlMonitor rcm(my_remote_pressed); int runRCM(int argc, char *argv) { rcm.runPressed(); return 0; } int main(int argc, char **argv) { execi(&runRCM, 0, 0, PRIO_NORMAL + 1, DEFAULT_STACK_SIZE); return 0; } The error is RemoteControlMonitor.H:58: invalid type void *' for default argument to void ()(unsigned int)' rcx1.C: In function void __static_initialization_and_destruction_0(int, int)': rcx1.C:54: ANSI C++ forbids implicit conversion from void *' in default argument What can I do next?
which line is 58? I guess you you can try to cast the NULL. (keyaction)NULL
2

you do not need to dereference you pointer, *rph, just call it as normal as rph and it should work fine, other wise you are trying to set the pointer *rph to pressed and not its value.

The *rph = pressed means set my memory location to pressed, where rph = pressed means set my value to pressed.

This link has some handy information in regards to references, pointers and dereferencing.

Hope this helps:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.