【发布时间】:2020-12-24 09:20:12
【问题描述】:
我正在尝试在 c++ winRT 组件中使用一维数组
类.idl
namespace MyClass
{
[default_interface]
runtimeclass Class
{
Class();
String[] aaa(String[] v);
}
}
类.h
#pragma once
#include "Class.g.h"
namespace winrt::MyClass::implementation
{
struct Class : ClassT<Class>
{
Class() = default;
hstring[] aaa(hstring[] s);
};
}
namespace winrt::MyClass::factory_implementation
{
struct Class : ClassT<Class, implementation::Class>
{
};
}
类.cpp
#include "pch.h"
#include "Class.h"
#include "Class.g.cpp"
namespace winrt::MyClass::implementation
{
hstring[] Class::aaa(hstring[] s)
{
return null;
}
}
然后在Class.h中报错
Class.cpp 中的更多错误
欢迎您的评论
更新问题
类.idl
namespace HTMLInterpreter
{
[default_interface]
runtimeclass Class
{
Class();
void aaa(Int32[] value);
}
}
类.h
#include "Class.g.h"
namespace winrt::HTMLInterpreter::implementation
{
struct Class : ClassT<Class>
{
Class() = default;
void aaa(int[] v);
};
}
namespace winrt::HTMLInterpreter::factory_implementation
{
struct Class : ClassT<Class, implementation::Class>
{
};
}
类.cpp
void Class::aaa(int[] v)
{
}
即使我把hstring[]改成int[],也会报类似的错误
【问题讨论】:
-
hstring[]不是合法的 C++。在 Generated Files\Sources 目录中查找可用作起点的示例代码。 -
@Raymond Chen,我认为hstring不是重点,即使我将String []更改为Int32 [],将hstring []更改为int [](请参阅底部的更新问题的问题),它报告了类似的错误
-
C++/WInRT 仍然是 C++。
int[] v不是在 C++ 中声明整数数组的方式,int[] function()不是在 C++ 中声明返回整数数组的函数的方式。 (正如我所指出的,Sources目录中的文件提供了正确的声明。) -
@Raymond Chen 非常感谢,我明白了!它应该是 com_array
aaa(hstring const& value),一个新问题是我找不到向/从 com_array 添加/移动项目的方法,docs.microsoft.com/en-us/uwp/cpp-ref-for-winrt/com-array 的描述很简单 -
com_array 是一个固定大小的容器。您不会向其中添加项目;大小是固定的。您所做的是以所需的大小构建它。例如,您可以将值添加到 std::vector,然后从向量构造 com_array。该页面显示“如果您正在编写 API,那么您可能需要构造一个 winrt::com_array 以将投影数组返回给调用者。”十个左右的构造函数中的每一个都有文档,以及关于如何将它们与某些类型的迭代器一起使用以获得特定行为的建议。
标签: c++ uwp windows-runtime idl