[MC++ 2005]

M

marco_segurini

Hi,

the build of the following code returns me an error.

So I am wondering what is the right way to pass ta a function a
parameter that is a reference to an array item of value struct.

TIA.
Marco.

using namespace stdcli::language;

value struct MyInt
{
int i;
};

void Set(MyInt & item, int set)
{
item.i = set;
}

void Set(array<MyInt>^ vInt)
{
for (int Pos=0; Pos<vInt->Length; ++Pos)
{
Set(vInt[Pos], Pos);
}
}

int main()
{
array<MyInt>^ vInt = gcnew array<MyInt>(5);

Set(vInt);

return 0;
}

Value.cpp(17) : error C2665: 'Set' : none of the 2 overloads could
convert all the argument types
Value.cpp(8): could be 'void Set(MyInt &,int)'
while trying to match the argument list '(MyInt, int)'
 
I

Ioannis Vranos

marco_segurini said:
Hi,

the build of the following code returns me an error.

So I am wondering what is the right way to pass ta a function a
parameter that is a reference to an array item of value struct.

TIA.
Marco.

using namespace stdcli::language;

value struct MyInt
{
int i;
};

void Set(MyInt & item, int set)


void Set(MyInt % item, int set)
 
B

Ben Schwehn

marco_segurini said:
using namespace stdcli::language;

value struct MyInt
{
int i;
};

void Set(MyInt & item, int set)
{
item.i = set;
}

void Set(array<MyInt>^ vInt)
{
for (int Pos=0; Pos<vInt->Length; ++Pos)
{
Set(vInt[Pos], Pos);
}
}

int main()
{
array<MyInt>^ vInt = gcnew array<MyInt>(5);

Set(vInt);

return 0;
}

Value.cpp(17) : error C2665: 'Set' : none of the 2 overloads could
convert all the argument types
Value.cpp(8): could be 'void Set(MyInt &,int)'
while trying to match the argument list '(MyInt, int)'

since the array is controlled by the garbage collector you should either
use tracking references:

void Set(MyInt% item, int set)
{
item.i = set;
}

or pin the array beforehand:

pin_ptr<MyInt> pInt = &vInt[0];
for (int Pos=0; Pos < vInt->Length; ++pInt)
{
Set(*pInt, Pos);
}

hth
 
B

Ben Schwehn

for (int Pos=0; Pos < vInt->Length; ++pInt)
oops:
for (int Pos=0; Pos < vInt->Length; ++pInt, ++Pos)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top