-
January 9th, 2014, 09:13 PM
#1
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
-
January 10th, 2014, 12:21 AM
#2
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.
-
January 10th, 2014, 07:59 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|