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)'
Ioannis Vranos - 15 Oct 2004 21:20 GMT
> Hi,
>
[quoted text clipped - 14 lines]
>
> void Set(MyInt & item, int set)
void Set(MyInt % item, int set)

Signature
Ioannis Vranos
Ben Schwehn - 15 Oct 2004 21:21 GMT
> using namespace stdcli::language;
>
[quoted text clipped - 29 lines]
> 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

Signature
Ben
http://bschwehn.de
Ben Schwehn - 15 Oct 2004 21:26 GMT
> for (int Pos=0; Pos < vInt->Length; ++pInt)
oops:
for (int Pos=0; Pos < vInt->Length; ++pInt, ++Pos)