Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Meyers' implementation of a Singleton Attempting to reference a deleted function (paho-mqttpp3 library) (mqtt::async_client class)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
};

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading