Thread: returning struct

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    18

    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nothing wrong with returning a struct, but you can't print a struct (or "insert into cout" if you like).

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The returning part worked fine, actually. You would have had a much clearer error message if you tried to return the wrong thing or didn't return anything in the function.

    cout does not know how to properly display any user-defined type, unless operator << is overloaded.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
    #include <iostream>
    using namespace std;
    
    struct name {
        int first;
        int second;
    };
    
    name fun() {
        name space = {1, 2};
        return space;
    }
    
    ostream& operator<<(ostream& os, name n) {
        return os << n.first << ", " << n.second;
    }
    
    int main() {
        cout << fun() << '\n';
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning struct in function
    By xeon321 in forum C Programming
    Replies: 3
    Last Post: 06-24-2012, 01:50 AM
  2. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  3. Returning a pointer to a struct
    By iain in forum C Programming
    Replies: 1
    Last Post: 03-23-2005, 04:42 PM
  4. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM
  5. Returning multiple types w/o a struct
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2003, 11:04 PM