0

I'm pretty new to programming in C++. I've done some C# over the years, but I wouldn't say I am proficient at it. I'm trying to change some code from native c++ to C++/CX and keep hitting lots of compiler errors, specifically in concern to vectors. I've been reading through MSDN - Collections (C++/CX) and gathered that I need to use an IVector.

I have a struct defined in another header file:

typedef struct myStruct{
 float p;
 double x;
 double y;
 uint id;
}

I used a vector of this struct as a parameter in a method declaration:

void ProcessStruct (std::vector<myStruct> myStructs){}

When converting it to an IVector, like so:

void ProcessStruct (Windows::Foundation::Collections::IVector<myStruct>^ myStructs){}

I always end up getting compiler error C3225: "generic type argument for 'arg' cannot be 'type', it must be a value type or handle type". I tried using using IVector<myStruct^>^ instead, but then I just end up with C3699: "operator' : cannot use this indirection on type 'type'"

So I guessing my only option is to create a generic , but here I get very confused as to what I am actually supposed to be doing. How do I take a struct and turn it in to a generic? What is std::vector doing that IVector cannot?

4

1 回答 1

1

首先你需要使用 Platform::Collections::Vector 而不是 std::vector。它们的工作原理基本相同,因为它们的行为都类似于向量,但是 Platform::Collections::Vector 是一个 WRT 对象,这就是我们使用 ^(帽子指针)来处理它们的原因。

我像这样使用它:

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

private:
    Platform::Collections::Vector<int>^ NUM;
};

从某种意义上说,您将 IVector 用作属性(因为属性可以是公共成员),然后将其转换为 c++/cx Vector。稍后当您需要它时,使用 IVector 作为返回 Vector 的媒介。

另请注意,set 函数的参数是 IVector,get 函数也是 IVector 类型。

一些帮助的代码:)

void Something::SomeFunction(){
num = ref new Vector<int>;  //make num point to a new Vector<int>

num->Append(5);             //Take the number 5 cast it from IVector to Vector
                            //and store it in NUM. (The cast happens due to
                            //the code in the IVector property we made)

int TempNum = 7 + num->GetAt(0);    //Use num to get the first element of NUM and 
                                    //add 7 to it. (It's now 10, I mean 12)


num->InsertAt(0, T);                //Take T (12) and replace it with the first
                                    //element in NUM.
};

请记住,当我们对 num 做某事时,它会被强制转换,而不是对 NUM 做。这就是为什么有一个接口,它们帮助我们在 Java、C# 等之间使用向量(或任何其他东西,如字符串或映射)。

于 2013-09-09T01:26:57.220 回答