Returning Struct from a 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.
-
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.
-
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.
Well one problem is that in the AllData() function, you're returning a struct that is declared inside the function itself. This is no good because when the function exits, all the variables declared inside the function are destroyed, so you're returning something that no longer exists. It would be better if you wrote it as:
void MyData::GetData()
{
Fparams fParam;
AllData(fParam);
}Void MyData::AllData(Fparams& params)
{
params.i = 5;
params.flag = true;
}The difficult we do right away... ...the impossible takes slightly longer.
-
Well one problem is that in the AllData() function, you're returning a struct that is declared inside the function itself. This is no good because when the function exits, all the variables declared inside the function are destroyed, so you're returning something that no longer exists. It would be better if you wrote it as:
void MyData::GetData()
{
Fparams fParam;
AllData(fParam);
}Void MyData::AllData(Fparams& params)
{
params.i = 5;
params.flag = true;
}The difficult we do right away... ...the impossible takes slightly longer.
-
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.
You need to override the = opperator for the struct so that all the data is copied back. Or use a pointer and pass it to the func, which is better, and a lot lighter on the stack.
-
You need to override the = opperator for the struct so that all the data is copied back. Or use a pointer and pass it to the func, which is better, and a lot lighter on the stack.
Looking at the error there's probably a missing equality operator, but why's not obvious as we haven't got the full class definition. And it's not better to pass a pointer rather than returning a value - a decent compiler C++98 compiler will implement NRVO which makes it just as efficient to return a value.
-
Well one problem is that in the AllData() function, you're returning a struct that is declared inside the function itself. This is no good because when the function exits, all the variables declared inside the function are destroyed, so you're returning something that no longer exists. It would be better if you wrote it as:
void MyData::GetData()
{
Fparams fParam;
AllData(fParam);
}Void MyData::AllData(Fparams& params)
{
params.i = 5;
params.flag = true;
}The difficult we do right away... ...the impossible takes slightly longer.
-
Looking at the error there's probably a missing equality operator, but why's not obvious as we haven't got the full class definition. And it's not better to pass a pointer rather than returning a value - a decent compiler C++98 compiler will implement NRVO which makes it just as efficient to return a value.
And the code ends up different to what you intend...