mercredi 25 mars 2020

unique_ptr in a initializer list : No matching constructor for the initialization of std::unique_ptr

I have the following class LinkStats to extract link status information such as rx_error, rx_dropped, tx_error and tx_dropped.

    class LinkStats{

    LinkStats() {
        getifaddrs(&m_ifaddr);
        m_interfaceAddresses = { m_ifaddr, []( ifaddrs * x ) { freeifaddrs( x );}};
        m_interface = "eth0";
    };

    ~LinkStats() = default;

    uint32_t getRxError(){
        return (*getLinkStatus().get())->rx_errors;
    }

    uint32_t getTxError(){
        return (*getLinkStatus().get())->tx_errors;
    }

    uint32_t getRxDrop(){
        return (*getLinkStatus().get())->rx_dropped;
    }

    uint32_t getTxDrop(){
        return (*getLinkStatus().get())->tx_dropped;
    }

private:
    std::unique_ptr<rtnl_link_stats*> getLinkStatus() {
        for ( ifaddrs *ifa = m_interfaceAddresses.get(); ifa; ifa = ifa->ifa_next) {
            if ((ifa->ifa_addr->sa_family == AF_PACKET) &&
                (ifa->ifa_data != NULL) &&
                (ifa->ifa_addr != NULL) &&
                (0 == m_interface.compare(ifa->ifa_name))) {
                return std::make_unique<rtnl_link_stats*>(reinterpret_cast<rtnl_link_stats*>(ifa->ifa_data));
            }
        }
    }

    std::unique_ptr<ifaddrs, std::function<void(ifaddrs*)>> m_interfaceAddresses;
    ifaddrs *m_ifaddr;
    std::shared_ptr<Logger> m_logger;
    std::string m_interface;
};

class MyClass{

    MyClass():
    m_linkStatus(std::make_unique<LinkStats>())
    {

    }

private:
     std::unique_ptr<LinkStats> m_linkStatus;
};

I am getting the following error.

No matching construcot for the initialization of std::unique_ptr<LinkStats> what am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire