Search code examples
c++c++17windows-runtimec++-winrt

C++/WinRT DeviceInformation No default Constructor exists error


Hello i'm trying to port the following code from a c# program.



 public class DeviceListEntry
 {
     private DeviceInformation device;
     private String deviceSelector;

     public String InstanceId
     {
         get
         {
             return device.Properties[DeviceProperties.DeviceInstanceId] as String;
         }
     }

     public DeviceInformation DeviceInformation
     {
         get
         {
             return device;
         }
     }

     public String DeviceSelector
     {
         get
         {
             return deviceSelector;
         }
     }

     /// <summary>
     /// The class is mainly used as a DeviceInformation wrapper so that the UI can bind to a list of these.
     /// </summary>
     /// <param name="deviceInformation"></param>
     /// <param name="deviceSelector">The AQS used to find this device</param>
     public DeviceListEntry(Windows.Devices.Enumeration.DeviceInformation deviceInformation, String deviceSelector)
     {
         device = deviceInformation;
         this.deviceSelector = deviceSelector;
     }

 }
}

Essentially if I attempt to port the DeviceListEntry Constructor with the deviceInformation object it errors me saying there is no default Constructor for the DeviceInformation Class.

however if I remove the corresponding code for the device object the DevuceListEntry constructor doesn't return any errors.

Here's what I have:

#include "pch.h"
#include "DeviceListEntry.h"
#include "Constants.h"
using namespace winrt::Windows::Devices::Enumeration;
using namespace winrt::Windows::Foundation::Collections;

namespace SerialArduino
{

    const winrt::hstring DeviceProperties::DeviceInstanceID = {L"System.Devices.DeviceInstanceId"};


    // having a DeviceInformation object as parameter as well as object causes no default constructor error
    DeviceListEntry::DeviceListEntry(winrt::Windows::Devices::Enumeration::DeviceInformation deviceInformation, winrt::hstring deviceSelector)
    {
        device = deviceInformation;
        this->deviceSelector = deviceSelector;
    }

    winrt::hstring DeviceListEntry::InstanceId()
    {

        return winrt::unbox_value<winrt::hstring>(device.Properties().Lookup(DeviceProperties::DeviceInstanceID));
    
    }

    winrt::Windows::Devices::Enumeration::DeviceInformation DeviceListEntry::DeviceInformation()
    {
        return device;
    }

    winrt::hstring DeviceListEntry::DeviceSelector()
    {
        return deviceSelector;
    }

}

Error:

Error (active)  E0291   no default constructor exists for class 
Error   C2512   'winrt::Windows::Devices::Enumeration::DeviceInformation': no appropriate default constructor available SerialArduino

Any help would be greatly appreciated as I'm completely lost on what I'm doing wrong. Thanks in advance


Solution

  • Assuming your DeviceListEntry declaration looks like:

    namespace SerialArduino {
    
    class DeviceListEntry {
    public:
        //...
    
    private:
        winrt::Windows::Devices::Enumeration::DeviceInformation device;
        winrt::hstring deviceSelector;
    };
    
    }
    

    Try implementing your constructor like this:

    // having a DeviceInformation object as parameter as well as object causes no default constructor error
    DeviceListEntry::DeviceListEntry(winrt::Windows::Devices::Enumeration::DeviceInformation deviceInformation, winrt::hstring deviceSelector_)
        : device(deviceInformation), deviceSelector(deviceSelector_)
    { }
    

    This way, device will be copy-initialized from deviceInformation.

    Otherwise, the compiler will try to default-construct device and then copy-assign deviceInformation to it.