0

从 C++ 到 C++/CX 的转变我偶然发现 ref 类不能在它们是公共或受保护的成员中包含本机成员,因为 java 和其他东西可能存在错误。相反,我们现在必须使用属性,我可以制作但只能保存 1 个值...

想法是创建一个属性,将 4 个浮点数存储在数组或向量中,然后将值传递给 XMVECTOR。到目前为止,我在类头文件中的相关代码是:

public:
property std::vector<float> num{
        void set(std::vector<float> e){
            NUM = e;
        };
        std::vector<float> get(){
            return NUM;
        };
    };
private:
std::vector<float> NUM;

稍后在 .cpp 文件中,我执行以下操作:

std::vector<float> g;
g.pushback(3);
num = g;

我还将它作为一个字符串传递给 TextBox(但这并不重要)。最后我得到了很多类似的错误...... 2个错误是:

error C3986: 'set': signature of public member contains native type 'std::vector<_Ty>'
error C3986: 'set': signature of public member contains native type 'std::allocator<_Ty>'

我唯一想象的是我不能使用字符串或向量。我知道 Platform::Strings 存在但是向量呢?

4

3 回答 3

3

标准 C++ 类型无法跨 WinRT ABI 进行投影,WinRT ABI 是所有 WinRT 语言投影 (C#/VB/JS) 共享的通信层。正如 Jagannath 提到的,有一个集合接口 ( Windows::Foundation::Collections::IVector<T>)。还有一个字典类型 ( IMap<K,V>) 以及各种迭代器和支持类型。所有语言都可以理解这些,但只是接口。每个语言投影都负责编写实现这些接口的运行时类。对于 C++/Cx,这些 ref 类位于标头<collection.h>中,并且位于 namespace 中Platform::CollectionsPlatform::Collections::Vector<T>并且Platform::Collections::Map<K,V>是您可以用作后备存储的基本类型。此外,Vector<T>可以从std::vector<T>.

但是,您也不能创建类型的公共属性Platform::Collections::Vector<T>,因为这仍然是 C++ 类型。相反,您要做的是创建一个类型的公共属性,该属性由类型Windows::Foundation::Collection::IVector<T>的私有成员变量支持Platform::Collections::Vector<T>

本质上:

public:
property Windows::Foundation::Collections::IVector<float>^ num{
        Windows::Foundation::Collections::IVector<float>^ get(){
            return NUM;
        }
    }
private:
Platform::Collections::Vector<float>^ NUM;

我避免提及属性设置器,因为这很棘手(您的私有类型也需要是 an并且只有当它来自 C++IVector时才会是 a )。Platform::Collections::Vector

于 2013-07-17T17:10:52.163 回答
0

嘿伙计们,我设法让这个工作,所以我想我会发布代码。头文件是这样的:

public:
property Windows::Foundation::Collections::IVector<int>^ num{
    void set(Windows::Foundation::Collections::IVector<int>^ e){
        NUM = safe_cast<Platform::Collections::Vector<int>^>(e);
    };
    Windows::Foundation::Collections::IVector<int>^ get(){
        return NUM;
    };
};
private:
Platform::Collections::Vector<int>^ NUM;

在 .cpp 文件中,代码是:

num = ref new Vector<int>;  
num->Append(5);
num->Append(54);
TextBox1->Text = num->GetAt(0).ToString() + "\n" + num->GetAt(1).ToString();

结果会将值 5 和 54 写入 TextBox。

于 2013-07-18T00:00:13.793 回答
0

您可以Windows::Foundation::Collections::IVector<float>^用于签名。我无法对此进行测试,因为我手头没有编译器。

于 2013-07-17T06:28:52.073 回答