Home > Software engineering >  Meyers' implementation of a Singleton Attempting to reference a deleted function (paho-mqttpp3
Meyers' implementation of a Singleton Attempting to reference a deleted function (paho-mqttpp3

Time:12-04

OS : Windows 10 x64
Build Tool : Visual Studio 2021
Language Standard : C 20
paho-mqttpp3 : 1.2.0
Package Manager : vcpkg

I am trying to build a mqtt::async_client using paho-mqttpp3 verrsion 1.2.0

I am using Meyers' implementation of a Singleton for my MQTT Client. Reference : https://stackoverflow.com/a/17713799/6319901

I am getting the following error

Error C2280 'MqttClient::MqttClient(void)': attempting to reference a deleted function

on line static MqttClient instance;

When I get my mouse over the instance (object) the tool-tip displays the following error.

Error (active) E1790 the default constructor of "MqttClient" cannot be referenced -- it is a deleted function

Source :

MqttClient& MqttClient::get_instance(void)
{
     static MqttClient       instance;
     return instance;
}

Header:

class MqttClient : public virtual mqtt::callback
{
private:
    mqtt::async_client                  client;
    void                                connected(const std::string& cause) override;
    void                                connection_lost(const std::string& cause) override;
    void                                delivery_complete(mqtt::delivery_token_ptr tok) override;
    void                                message_arrived(mqtt::const_message_ptr msg) override;
    MqttClient() = default;
    ~MqttClient() = default;
public:
    static MqttClient&                  get_instance(void);
    MqttClient(const MqttClient& obj) = delete;
    MqttClient(MqttClient&& obj) = delete;
    MqttClient& operator=(const MqttClient& obj) = delete;
    MqttClient& operator=(MqttClient&& obj) = delete;
};

CodePudding user response:

From the documentation it appears, that mqtt:async_client is not default-constructible, meaning that you would have to provide an initializer in MqttClient's constructor or a default member initializer. Not doing so results in the default constructor being deleted, despite your attempt to explicitely default it.

  •  Tags:  
  • c
  • Related