0

I'm currently writing a program in CLI/C++ using an imported C# package. I need to use one of the functions in one of the C# objects which takes in an array. Unfortunately it isn't allowing me to use a CLI array, defined like so:

array<float>^ knots = gcnew array<float>(nurbs->GetNumKnots());

(and then populated in a loop).

The object and the function are:

TRH::NURBSCurveKit^ nurbsKit = gcnew TRH::NURBSCurveKit();
nurbsKit->SetKnots(nurbs->GetNumKnots(), knots);

This returns an error basically saying that the cli::array type isn't compatible. Does anyone know of a way where I can cast the array, or possibly define it differently?

I'm quite new to CLI so I'm a little vague on the way it handles things at times.

Thanks

(I do something similar later on using an array of TRH::Points, but they're not defined as references or pointers, so I'm not sure if they'd work with any solutions or not).

1
  • NURBSCurveKit is a C++ library. Why you are using a C# wrapper (??) for it is very unclear. It certainly doesn't look like a very good wrapper if it won't take an array<float>^. You can't get help unless you avoid us having to guess at this wrapper and what its SetKnots() method looks like. Commented Dec 5, 2013 at 13:20

2 Answers 2

1

I'm not sure if it's the same NURBSCurveKit, but according to an online API reference I found, the SetKnots method takes one parameter, not two. Since a managed array knows how long it is, you generally don't have to pass in a length with an array.

If this matches your API, just switch to pass a single parameter to SetKnots. (The reference I found uses a different namespace, so it may not be what you're using.)

array<float>^ knots = gcnew array<float>(nurbs->GetNumKnots());

TRH::NURBSCurveKit^ nurbsKit = gcnew TRH::NURBSCurveKit();
nurbsKit->SetKnots(knots);
Sign up to request clarification or add additional context in comments.

2 Comments

Huh, it is the same (at least the function works with one argument). An example I had used two arguments, I guess I just jumped to the conclusion that the array was causing the problem. Which...yeah, that was dumb. Thanks very much.
As Hans Passant noted, NURBSCurveKit is a C++ library. Your example is probably using unmanaged C++, where you do need to pass in the length of arrays.
0

This is my test case, seems everything is fine.

C#

namespace CS
{
    public class Test
    {
        public int GetNum()
        {
            return 5;
        }

        public void ShowArray(int num, float[] arr)
        {
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("[{0}] = {1}",i,arr[i]);
            }
        }
    }
}

C++/CLI

using namespace System;
using namespace CS;

int main(array<System::String ^> ^args)
{
    Test^ test = gcnew Test();
    array<float>^ arr = gcnew array<float>(test->GetNum());

    for (int i = 0; i < test->GetNum(); i ++)
    {
        arr[i] = (float)i * i;
    }
    test->ShowArray(test->GetNum(), arr);
    Console::ReadKey();
    return 0;
}

Dose you code have something different form mine?

1 Comment

Hmm, nothing obvious that I can see. It's still saying the cli::array type isn't valid though. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.