Compile error with function pointer in a class vector

Can anyone explain why the test code listed below compiles with the error?

mctest.cpp:3:35: error: 'mqttCallback_t' has not been declared
 void addSubscription(char* topic, mqttCallback_t cback);
                                   ^

Note that 3:35 points to the typedef … not the line actually printed with the error.

I’m using a vector to store MQTT topics and a callback function pointer for each topic. The call back functions will not be members of the class.

#include "Particle.h"
#include <vector>
typedef void (*mqttCallback_t)(char*, byte*, unsigned int);

class MqttClient{
    struct MqttSubscription {
        char topic[64];
        mqttCallback_t callback;
    };
  private:
    byte brokerIP[4];
    uint16_t maxPacketLength;	
    bool sessionActive;
    std::vector<MqttSubscription> subList;
  public:
    void addSubscription(char* topic, mqttCallback_t cback) {
        MqttSubscription sub;
        strncpy(sub.topic, topic, sizeof(sub.topic));
        sub.callback = cback;
        this->subList.push_back(sub);
    }
};



void setup() {

}

void loop() {

}

I believe it isn’t working because of the way the preprocessor works in the compilation process. An attempt is made to declare the class forward in some fashion and then that moves it before the typedef, which is not moved. If you move your typedef into a header included by this file it compiles for me.

1 Like

Thank you … Moving the typedef to a .h file solved my problem, and it’s nice to have a sense for why that made a difference. I greatly appreciate your help !!!