Click to See Complete Forum and Search --> : Constructor receiving struct as parameter


MegaCode535
September 3rd, 2010, 09:49 AM
Does anyone know if I can send a struct as parameter into a constructor?

Zaccheus
September 3rd, 2010, 10:42 AM
Like this?



value struct S
{
System::Int32 n;
};

ref class C
{
S myS;

public:
C(S s)
: myS(s)
{
}
};

MegaCode535
September 3rd, 2010, 11:25 AM
It didn´t work for me.

Zaccheus
September 3rd, 2010, 11:43 AM
Did it not compile???

What was the error?

MegaCode535
September 3rd, 2010, 12:00 PM
error C2664: cannot convert parameter from ':Form1::S ^' to 'Form2::S ^'

Of course the struct name has been replaced, but it was declared into the two classes.

Zaccheus
September 3rd, 2010, 12:17 PM
What did you compile? Neither ^ nor 'Teste' apears anywhere in my code. Have you tried compiling my code on its own?

Zaccheus
September 3rd, 2010, 12:39 PM
My code compiles as it is. Why not try that first? The ^ should not be in there.

error C2664: cannot convert parameter from ':Form1::S ^' to 'Form2::S ^'

Of course the struct name has been replaced, but it was declared into the two classes.

If you declared S inside Form1 and Form2 then Form1 and Form2 are two different types.

MegaCode535
September 3rd, 2010, 12:53 PM
Yes, but the structs are the same. This doesn´t work even with the cast "Form1::S^"

Zaccheus
September 3rd, 2010, 03:51 PM
No, they are two separate types. Also stop using ^ it is wrong.

Does my code compile on your compiler or not?

MegaCode535
September 3rd, 2010, 07:07 PM
No, it doesn´t.

Zaccheus
September 4th, 2010, 05:38 AM
What is the error message when you compile my code?