Home > front end >  Basic C problem - Using variable within the class
Basic C problem - Using variable within the class

Time:07-30

I have problem with C class. Is there have any way to reuse variable in the same class in the header file?

I have try

PubSubClient mqttClient(this->secureClient);
PubSubClient mqttClient(mqtt.secureClient);

but fail. Why?

#ifndef __MQTT_H_
#define __MQTT_H_

#include "Arduino.h"
#include "WiFi.h"
#include "WiFiClientSecure.h"
#include "PubSubClient.h"

class MQTT
{
public:

bool initWiFi();
String macAddress6btye(void);
void callback(char *topic, byte *payload, unsigned int length);
bool mqttConnect();
bool mqttPublish(const String &endPoint, const String &payload);
int getStrength(uint8_t points);

private:
    WiFiClientSecure secureClient;
    PubSubClient mqttClient(this->secureClient);
};

#endif

CodePudding user response:

The declaration

PubSubClient mqttClient(this->secureClient);

is treated as a function declaration. An invalid one.

If you want to initialize the member variable (using other member variables), you need to do it with a constructor initializer list:

class MQTT
{
public:
    MQTT()
        : mqttClient(secureClient)  // Initialization here
    {
        // Empty function body
    }

    // ...

    PubSubClient mqttClient;  // No initialization here
};
  •  Tags:  
  • c
  • Related