-
October 24th, 2013, 01:53 PM
#1
Returning struct from function
Hello all,
In my Header file:
class MyData
{
struct Fparams
{
int i;
bool flag;
};
}
Now in CPP file:
void MyData::GetData()
{
Fparams fParam;
fparam = AllData();
}
MyData::Fparams MyData::AllData()
{
Fparams fparam;
fparam.i = 5;
fparam.flag = true;
return fparam;
}
Now i get an error saying - error: no match for 'operator=' in fparam = AllData(); ........
What's wrong here? How can i fix this?
Thanks in advance.
-
October 24th, 2013, 02:20 PM
#2
Re: Returning struct from function
When you return fparam, you return a copy, and the compiler doesn't know how to copy it. You'll need to define an = operator. More typically though, you'll just pass the struct as a output parameter.
Code:
void MyData::GetData()
{
Fparams fParam;
fparam = AllData(fParam);
}
void MyData::AllData( Fparams& fParam)
{
fparam.i = 5;
fparam.flag = true;
}
-
October 24th, 2013, 02:26 PM
#3
Re: Returning struct from function
How can i define a =operator for this struct?
-
October 24th, 2013, 02:35 PM
#4
Re: Returning struct from function
Originally Posted by Don Guy
What's wrong here?
The whole code snippet you have posted does not make any sense!
Both MyData methods you showed use local Fparams variables that go out of scope after methods return.
Originally Posted by Don Guy
How can i fix this?
Fix these design bugs.
Use code tags while posting code snippets. Otherwise your code is unreadable.
Victor Nijegorodov
-
October 24th, 2013, 02:41 PM
#5
Re: Returning struct from function
Originally Posted by VictorN
The whole code snippet you have posted does not make any sense!
Both MyData methods you showed use local Fparams variables that go out of scope after methods return.
Fix these design bugs.
Use code tags while posting code snippets. Otherwise your code is unreadable.
That was my thought as well, but I assume he's just posting a simplified example.
-
October 24th, 2013, 02:43 PM
#6
Re: Returning struct from function
Originally Posted by Don Guy
How can i define a =operator for this struct?
Search for C++ overload assignment operator, or something like that. Using an output parameter as I illustrated is simpler and pretty common.
-
October 24th, 2013, 02:50 PM
#7
Re: Returning struct from function
Originally Posted by GCDEF
That was my thought as well, but I assume he's just posting a simplified example.
Perhaps...
But posting "a simplified example" that does nothing and is meaningless seems to be disrespect to the Forum, IMHO.
Victor Nijegorodov
-
October 24th, 2013, 03:27 PM
#8
Re: Returning struct from function
Originally Posted by Don Guy
Hello all,
In my Header file:
1) Please use code tags when posting code. You should know by now that code posted without code tags winds up looking like a mess.
2) Post the actual code you're testing. The code you posted can never work, since it could never compile.
Code:
class MyData
{
struct Fparams
{
int i;
bool flag;
};
} // <-- Missing semicolon
Code:
void MyData::GetData() //???? Where did this function come from??
//..
MyData::Fparams MyData::AllData() // And this one ??
If you get a compiler error, it is imperative you post the code as-is, directly from the editor. Don't type the code in -- actually copy and paste the code in the forum message. Also, you should post the code that allows us to take the code directly from your message, and without any edits from us, copy it into our compiler window and compile.
Otherwise, we won't know if your real code has all of these typos and missing entities that you're showing in the message, and we go down the wrong path as to the cause of the compiler error. For example, you have missing semicolons, and those functions could never exist since they are not defined in the MyData class.
As to your question, a struct can be returned. Now I have no idea what that code you posted is supposed to show, due to the things I pointed out.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 24th, 2013 at 03:33 PM.
-
October 25th, 2013, 06:43 AM
#9
Re: Returning struct from function
When you return fparam, you return a copy, and the compiler doesn't
know how to copy it. You'll need to define an = operator.
If the user doesn't supply one, doesn't the compiler automatically
generate a copy assignment operator that does a member by member copy ?
(Unless there is a user declared move constructor and/or a user declared
move assignment operator.)
-
October 25th, 2013, 08:05 AM
#10
Re: Returning struct from function
Originally Posted by VictorN
The whole code snippet you have posted does not make any sense!
Both MyData methods you showed use local Fparams variables that go out of scope after methods return.
Fix these design bugs.
There is nothing wrong here since he's returning an object instance, the local objects are copied (hence the need for an operator=). Depending on what's inside this object this may even benefit from an assignment operator with move semantics.
It would be an error if he returned a pointer of reference to a local object.
whether it is optimal to return this particular class as an object or whether returning via parameter would be more beneficial is another discussion entirely.
-
October 25th, 2013, 09:52 AM
#11
Re: Returning struct from function
Originally Posted by Philip Nicoletti
If the user doesn't supply one, doesn't the compiler automatically
generate a copy assignment operator that does a member by member copy ?
(Unless there is a user declared move constructor and/or a user declared
move assignment operator.)
Yes, that's why a struct instance can be returned in this case, unless that struct has copying turned off by doing the usual things (making copy/assignment private, deriving from a base class that makes the object non-copyable, etc.).
However, the OP says they get that error, so posting the real code is required.
Regards,
Paul McKenzie
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
|