C++/WinRT DeviceInformation No default Constructor exists 错误
C++/WinRT DeviceInformation No default Constructor exists error
您好,我正在尝试从 C# 程序移植以下代码。
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;
}
}
}
基本上,如果我尝试使用 deviceInformation 对象移植 DeviceListEntry 构造函数,它会错误地提示我没有 DeviceInformation 的默认构造函数 Class。
但是,如果我删除设备对象的相应代码,DevuceListEntry 构造函数不会 return 任何错误。
这是我的:
#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 (active) E0291 no default constructor exists for class
Error C2512 'winrt::Windows::Devices::Enumeration::DeviceInformation': no appropriate default constructor available SerialArduino
任何帮助将不胜感激,因为我完全不知道自己做错了什么。
提前致谢
假设您的 DeviceListEntry
声明如下:
namespace SerialArduino {
class DeviceListEntry {
public:
//...
private:
winrt::Windows::Devices::Enumeration::DeviceInformation device;
winrt::hstring deviceSelector;
};
}
尝试像这样实现你的构造函数:
// 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_)
{ }
这样,device
将从 deviceInformation
变为 copy-initialized。
否则,编译器会尝试default-constructdevice
然后copy-assigndeviceInformation
到它。
您好,我正在尝试从 C# 程序移植以下代码。
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;
}
}
}
基本上,如果我尝试使用 deviceInformation 对象移植 DeviceListEntry 构造函数,它会错误地提示我没有 DeviceInformation 的默认构造函数 Class。
但是,如果我删除设备对象的相应代码,DevuceListEntry 构造函数不会 return 任何错误。
这是我的:
#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 (active) E0291 no default constructor exists for class
Error C2512 'winrt::Windows::Devices::Enumeration::DeviceInformation': no appropriate default constructor available SerialArduino
任何帮助将不胜感激,因为我完全不知道自己做错了什么。 提前致谢
假设您的 DeviceListEntry
声明如下:
namespace SerialArduino {
class DeviceListEntry {
public:
//...
private:
winrt::Windows::Devices::Enumeration::DeviceInformation device;
winrt::hstring deviceSelector;
};
}
尝试像这样实现你的构造函数:
// 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_)
{ }
这样,device
将从 deviceInformation
变为 copy-initialized。
否则,编译器会尝试default-constructdevice
然后copy-assigndeviceInformation
到它。