'How to avoid "warning converting from" in C++? [duplicate]

I have the following class:

MIDIDevice midi;
class ExampleMidi
{
protected:
    void noteOnHandler(byte channel, byte note, byte velocity)
    {
        Serial.print("Note On, ch=");
        Serial.print(channel, DEC);
        Serial.print(", note=");
        Serial.print(note, DEC);
        Serial.print(", velocity=");
        Serial.println(velocity, DEC);
    }
protected:
    void init()
    {
        midi.setHandleNoteOn((void (*)(byte, byte, byte)) & ExampleMidi::noteOnHandler);
    }
}

midi.setHandleNoteOn expect a point to a function, or I would like to pass a method to it. This cast is working, it compile and there is no error during execution but I am getting the following warning:

warning: converting from 'void (ExampleMidi::*)(byte, byte, byte) {aka> void (ExampleMidi::*)(unsigned char, unsigned char, unsigned char)}' to 'void (*)(byte, byte, byte) {aka void (*)(unsigned char, unsigned char, unsigned char)}' [-Wpmf-conversions]        midi.setHandleNoteOn((void (*)(byte, byte, byte)) & ExampleMidi::noteOnHandler);

How can I get rid of this warning?



Solution 1:[1]

Following the comment from @FrançoisAndrieux I managed to get rid of the warning:

void noteOnHandler(byte channel, byte note, byte velocity);

MIDIDevice midi;
class ExampleMidi
{
public:
    void init()
    {
        midi.setHandleNoteOn(noteOnHandler);
    }
    void noteOn(byte channel, byte note, byte velocity)
    {
        Serial.print("Note On, ch=");
        Serial.print(channel, DEC);
        Serial.print(", note=");
        Serial.print(note, DEC);
        Serial.print(", velocity=");
        Serial.println(velocity, DEC);
    }
}

ExampleMidi example;

void noteOnHandler(byte channel, byte note, byte velocity)
{
    example.noteOn(channel, note, velocity);
}

There might be some way to make it better but for the moment it is how I got it to work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexandre