CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2010
    Posts
    7

    Question Constructor receiving struct as parameter

    Does anyone know if I can send a struct as parameter into a constructor?

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Constructor receiving struct as parameter

    Like this?

    Code:
    value struct  S
    {
    	System::Int32 n;
    };
    
    ref class C
    {
    	S	myS;
    
    public:
    	C(S s)
    	:	myS(s)
    	{
    	}
    };
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Aug 2010
    Posts
    7

    Re: Constructor receiving struct as parameter

    It didn´t work for me.

  4. #4
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Constructor receiving struct as parameter

    Did it not compile???

    What was the error?
    My hobby projects:
    www.rclsoftware.org.uk

  5. #5
    Join Date
    Aug 2010
    Posts
    7

    Re: Constructor receiving struct as parameter

    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.
    Last edited by MegaCode535; September 3rd, 2010 at 12:17 PM.

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Constructor receiving struct as parameter

    What did you compile? Neither ^ nor 'Teste' apears anywhere in my code. Have you tried compiling my code on its own?
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Constructor receiving struct as parameter

    My code compiles as it is. Why not try that first? The ^ should not be in there.

    Quote Originally Posted by MegaCode535 View Post
    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.
    Last edited by Zaccheus; September 3rd, 2010 at 12:43 PM.
    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Aug 2010
    Posts
    7

    Angry Re: Constructor receiving struct as parameter

    Yes, but the structs are the same. This doesn´t work even with the cast "Form1::S^"

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Constructor receiving struct as parameter

    No, they are two separate types. Also stop using ^ it is wrong.

    Does my code compile on your compiler or not?
    My hobby projects:
    www.rclsoftware.org.uk

  10. #10
    Join Date
    Aug 2010
    Posts
    7

    Re: Constructor receiving struct as parameter

    No, it doesn´t.

  11. #11
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Constructor receiving struct as parameter

    What is the error message when you compile my code?
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured