Click to See Complete Forum and Search --> : Problem dealing with a function definition


hellhammer
October 27th, 2004, 04:47 PM
Hi,

I'm trying to port some code I wrote using DirectSound in C# to Visual C++ (managed). I've got most of it done except for this problem I'm stuck at. I understand that this is a newbie question, so please be gentle :)

The function I'm having a problem with is CaptureBufferObject->Read(arguments) in C++.

In C#, the function protoype is:


public Array Read(
int bufferStartingLocation,
Type returnedDataType,
LockFlag flag,
int[] ranks
);


My code has the following usage of the Read method.

MemBuffShort = (short[])(StreamCaptureBuffer.Read(StreamCapBuffReadPos, typeof(short), LockFlag.None, 50000));


where MemBuffShort = new short[100000];

In C++, the function prototype is:

public: Array* Read(
int bufferStartingLocation,
Type *returnedDataType,
LockFlag flag,
int ranks __gc[]
);


My problem is with the last parameter: int ranks __gc[]

I tried the following:

// With the variable declarations
static System::Int32 CapBuffPara[] = new System::Int32[] { 50000 };

// Calling the Read method
MemBuff = StreamCapBuffer->Read(StreamCapBuffReadPos, __typeof(int), LockFlag::None, CapBuffPara);


However, I get the following error message:

error C2440: '=' : cannot convert from 'System::Array __gc *' to 'int __gc[]'

How do I solve this problem? What needs to be changed?

Thanks for your help!

Jinto
October 27th, 2004, 05:27 PM
int CapBuffPara __gc[] = new int __gc[50000];

That creates a managed array of ints. That can be passed to that function.

hellhammer
October 27th, 2004, 05:32 PM
Jinto, thanks for the reply!

The array has only one element, whose value is 50000.

I apparently need to declare an array because the C++ implementation of the function requires an array as the fourth parameter.

I tried this:
int CapBuffPara __gc[] = new int __gc[]{50000};

However, I got the following error.

error C3845: 'vcALPS::Form1::CapBuffPara': only static data members can be initialized inside a __gc class or value type

Where and how should I include this code?

lloydy
October 27th, 2004, 06:45 PM
Your prototype hould be :


public: Array* Read(
int bufferStartingLocation,
Type *returnedDataType,
LockFlag flag,
int ranks __gc[]
);


And then where you call


// Calling the Read method
int CapBuffPara __gc[] = {5000};
MemBuff = StreamCapBuffer->Read(StreamCapBuffReadPos, __typeof(int), LockFlag::None, CapBuffPara);

hellhammer
October 27th, 2004, 06:52 PM
I tried that but I get the following error:

error C3845: 'vcALPS::Form1::CapBuffPara': only static data members can be initialized inside a __gc class or value type

If I declare the array as:

static int CapBuffPara __gc[] = {5000};

I get the following error:

error C2440: '=' : cannot convert from 'System::Array __gc *' to 'int __gc[]'

Am I declaring the array at the wrong place? It's declared as a private member variable:


public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected: // .....

private: // .......
int CapBuffPara __gc[] = {50000};

//.... rest of the code
}

lloydy
October 27th, 2004, 08:11 PM
It seems that you are declaring CapBuffPara as a member variable. If this is the case you will not be able initialise in this way.

The code to declare it will be:


private: int CapBuffPara __gc[]


Then in Form1(void):


Form1(void)
{
CapBuffPara = new int __gc[1];
CapBuffPara[0] = 5000;
InitializeComponent();
}


Let me know how this goes

hellhammer
October 28th, 2004, 11:09 AM
Hi Llyody,

Still the same error :(


error C2440: '=' : cannot convert from 'System::Array __gc *' to 'int __gc[]'


1. The tooltip on CapBuffPara shows:
int (ProjName)::Form1::CapBuffPara __gc[]

2. The function definition requires the last argument to be int __gc[]

So I don't understand why the compiler says it cannot convert from System::Array __gc * to int __gc.

hellhammer
October 28th, 2004, 01:01 PM
I think I've fixed it... I overlooked the typecasting.

The line should be:

MemBuff = (System::Int32[])(StreamCapBuffer->Read(StreamCapBuffReadPos, __typeof(int), LockFlag::None, CapBuffPara));

Let me see if that works!