Tigershard
Tigershard

Reputation: 1

C++ native to C++/CX: How do I turn a std::vector into a IVector?

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?

Upvotes: 0

Views: 3643

Answers (1)

Jader J Rivera
Jader J Rivera

Reputation: 409

Well first thing first you need to use Platform::Collections::Vector rather than std::vector. They basically work the same since both behave like vectors, but Platform::Collections::Vector are a WRT object, that's why we use ^ (hat pointer) to handle them.

I use it like:

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;
};

In a sense you use a IVector as a property (because a property can be a public member) and then cast it into a c++/cx Vector. Later when you need it use the IVector as the medium to return the Vector.

Also notice that the parameter for the set function is an IVector and the get function is a IVector type too.

Some code to help :)

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.
};

Remember that when we do something to num it's casted and instead done to NUM. That's why there an interface, they're what help us use Vectors (or any other thing like String or Map) between Java, C# , etc.

Upvotes: 1

Related Questions