CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2013
    Posts
    19

    returning struct

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    struct name
    {
    int first;
    int second;
    
    
    };
    name fun()
    {
    name space;
    space.first=2;
    reurn space;
    }
    
    
    int main()
    {
    
    
    cout<<fun();
    }
    whats wrong in the program i tried to return struct.am i wrong

  2. #2
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Re: returning struct

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    struct name
    {
    int first;
    int second;
    };
    
    name fun()
    {
    name space;
    space.first=2;
    return space;
    }
    
    
    int main()
    {
    name Result;
    
    Result= fun();
    
    cout<< Result.first << Result.second;
    
    }
    The problem isn't the return (besides the typo in your code) of the structure but the attempt to pass the structure to cout. A quick solution is shown above - if you really want to cout your structure you need to overload the operator<< for your structure.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: returning struct

    Also: just because you CAN return a structure (or class) doesn't mean it's necessarily the smart thing to do.

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