Solved C++ struct function with return struct ...
-
Hi,
"result" is a local variable in "afunc". You cannot expect it to be known outside of "afunc". This is no restriction of Qt but a feature of the C++ language. But you can write something like this:a result=afunc(myvalue); myvar1 = result.x; myvar2 = result.y;
-Michael.
-
@m.sue Tanks A lot I have just read in an ebook this peculiarity of the struct .... I have many elements struct each one with a calculation and a return .... something like these:
struct a{ int x; double y; cv::Point cerr0; cv::Point cerr1; cv::Point cerr2; cv::Point cerr3; cv::Point result1() { return /* some long calculation*/}; cv::Point result2() {return /* some long calculation*/}; cv::Point result3() {return /* some long calculation*/}; } ........ afunc (cv::Point cerr0){ /* so in my file.cpp i can write these*/ some_value0 = afunc.result1; some_value1 = afunc.result2; some_value2 = afunc.result3;
In my case I think is a little bit more long as I expect .... :( ..so I try your suggest to try to simplify my function ...
Regards
Giorgio -
@gfxx ...NOT WORK ...
my real code:
/******************in my file.h*-***********************/ FindAngleCPointTarghet FindCentPoint( cv::Point2f Square_Points[4], bool a, bool b, bool c, bool d) { FindAngleCPointTarghet FindTarghet; F_lung1 = 0; F_lung2 = 0; F_lung3 = 0; F_lung4 = 0; F_lung5 = 0; F_barix = 0; F_bariy = 0; F_intbarix = 0; F_intbariy = 0; F_bariangle = 0; bool choose0 = false, choose1 = false, choose2 = false, choose3 = false; F_lung1 = std::pow((std::pow((Square_Points[0].x - Square_Points[1].x),2) + std::pow((Square_Points[0].y - Square_Points[1].y),2)), 0.5); F_lung2 = std::pow((std::pow((Square_Points[1].x - Square_Points[2].x),2) + std::pow((Square_Points[1].y - Square_Points[2].y),2)), 0.5); if (F_lung2 > F_lung1) { F_lung3 = F_lung2; F_lung4 = std::pow((std::pow((-Square_Points[1].x + Square_Points[2].x),2) + std::pow((0),2)), 0.5); F_lung5 = std::pow((std::pow((-Square_Points[1].y + Square_Points[2].y),2) + std::pow((0),2)), 0.5); F_barix = ((Square_Points[0].x + Square_Points[1].x + Square_Points[2].x + Square_Points[3].x)/4); F_bariy = ((Square_Points[0].y + Square_Points[1].y + Square_Points[2].y + Square_Points[3].y)/4); F_intbarix = round(F_barix); F_intbariy = round(F_bariy); F_bariangle = (std::atan2((Square_Points[2].y - Square_Points[1].y),(Square_Points[2].x - Square_Points[1].x))*180.00/3.1415926535); F_centrale = cv::Point(F_intbarix, F_intbariy); F_bariangle_template = F_bariangle; if(a) { .................. /*the calculation proceed for 180 row with the same variable declared here ... the result ( FindTarghet.Get_centrale etc) value change if a-b or a-d or etc etc bool value is true or false ....*/ .................. FindTarghet.Get_centrale = F_centrale; FindTarghet.Get_mezzo_rect_risc = F_mezzo_rect_risc; FindTarghet.Get_mezzo_rect_risc1 = F_mezzo_rect_risc1; FindTarghet.Get_mezzo_rect_risc_pt2 = F_mezzo_rect_risc_pt2; FindTarghet.Get_mezzo_rect_risc_pt3 = F_mezzo_rect_risc_pt3; FindTarghet.X_Point = F_intbarix; FindTarghet.Y_point = F_intbariy; FindTarghet.Angle = F_bariangle_template; FindTarghet.Major_Side1 = false; FindTarghet.Major_Side2 = false; FindTarghet.Get_Upper_Half_Square = false; FindTarghet.Get_Lower_Half_Square = false; return FindTarghet; /* than in my file.ccp*******************/ FindAngleCPointTarghet TarghetResutl = FindCentPoint(rect_points, targhet_verticale, targhet_orizzontale); centrale = TarghetResutl.Get_centrale; mezzo_rect_risc = TarghetResutl.Get_mezzo_rect_risc; mezzo_rect_risc1 = TarghetResutl.Get_mezzo_rect_risc1; mezzo_rect_risc_pt2 = TarghetResutl.Get_mezzo_rect_risc_pt2; mezzo_rect_risc_pt3 = TarghetResutl.Get_mezzo_rect_risc_pt3; intbarix = TarghetResutl.X_Point; intbariy = TarghetResutl.Y_point; bariangle = TarghetResutl.Angle; /***********the error .... 380 error signed .... a memeory allocation id/name problem I think****************/ (.bss+0x120):-1: error: multiple definition of `F_lung4' (.bss+0x128):-1: error: multiple definition of `F_lung3' .... etc etc.... all identical error but for all variable used during calculation .... }
I'm explain my better ... I try to make in file.h a struct function with struct return value .... As you see
-
If you include data in .h file, you can get this error
multiple definition of XXXIm not sure why this is so complex.
--
MYS.hstruct MyStruct { // DEFINE
int Value;
};MyStruct MyFunc(); // DEFINE
--
MYS.cppMyStruct MyFunc() { // IMPLEMENT
MyStruct localcopy;
localcopy.Value=100;
return localcopy;
}-- in some file--
#include "MYS.h"void buttonClick() {
MyStruct usedhere = MyFunc(); // USING}
-
MYS = MYQThread so....
-- MYQThread .h struct MyStruct { // DEFINE int Value; }; /* now I try to do these*/ MyStruct MyFunc(with parameter){some calculation with pnota bene arameter and some other local variable}; // DEFINE -- MYQThread .cpp MyStruct MyFunc() { // IMPLEMENT /* so your answer is : " in these local copy make your calculation" ... is right?*/ MyStruct localcopy; localcopy.Value=100; return localcopy; } -- in these MYQThread.cpp-- /* but my file.h and file.cpp is the same so is not possible do these ....*/ #include " MYQThread .h" void buttonClick() { MyStruct usedhere = MyFunc(); // USING }
If I understand well your reply you think mi struct function is utilize in other part of my app ... but is define in QThread.h and implement + utilized in QThread.cpp
Regards
GiorgioNote:
@mrjj perhaps your answer sounds like you'd better build a class that a struct func .... -
make sure that
MYQThread .cppMyStruct MyFunc(same params as in .h) {...}
No sure i understand
/* but my file.h and file.cpp is the same so is not possible do these*/why is file.cpp and file.h the same?
Also for the use
void buttonClick() {
MyStruct usedhere = MyFunc( ACTUAL PARAMETERS); // USING}
-
/***********MYQThread .h*****************/ struct FindACPTarghet{ cv::Point2f Square_Points[4]; bool Find_Upper_Half_Square; bool Find_Lower_Half_Square; cv::Point Get_centrale; cv::Point Get_mezzo_rect_risc; cv::Point Get_mezzo_rect_risc1; cv::Point Get_mezzo_rect_risc_pt2; cv::Point Get_mezzo_rect_risc_pt3; double X_Point; double Y_point; double Angle; bool Major_Side1; bool Major_Side2; bool Get_Upper_Half_Square; bool Get_Lower_Half_Square; //Get_Angle_Central_Point_Targhet GetDataTarghet; }/*FindCentPoint*/; class datathread : public QThread { Q_OBJECT public: explicit datathread(QObject *parent = 0, bool bthw = false); void run(); bool Stop; FindACPTarghet FindCentPoint( cv::Point2f Square_Points[4], bool Find_Upper_Half_Square, bool Find_Lower_Half_Square); /***********MYQThread .cpp*****************/ FindACPTarghet FindCentPoint( cv::Point2f Square_Points[4], bool Find_Upper_Half_Square, bool Find_Lower_Half_Square) { FindACPTarghet FindTarghet; <---------------------------------------- F_lung1 = 0; F_lung2 = 0; F_lung3 = 0; F_lung4 = 0; F_lung5 = 0; F_barix = 0; F_bariy = 0; F_intbarix = 0; F_intbariy = 0; F_bariangle = 0; bool choose0 = false, choose1 = false, choose2 = false, choose3 = false; F_lung1 = std::pow((std::pow((Square_Points[0].x - Square_Points[1].x),2) + std::pow((Square_Points[0].y - Square_Points[1].y),2)), 0.5); F_lung2 = std::pow((std::pow((Square_Points[1].x - Square_Points[2].x),2) + std::pow((Square_Points[1].y - Square_Points[2].y),2)), 0.5); if (F_lung2 > F_lung1) {.................. ........................ FindTarghet.Get_centrale = F_centrale; <--------------------------------- /*******************************undefined error reference**********************/ FindTarghet.Get_mezzo_rect_risc = F_mezzo_rect_risc; FindTarghet.Get_mezzo_rect_risc1 = F_mezzo_rect_risc1; FindTarghet.Get_mezzo_rect_risc_pt2 = F_mezzo_rect_risc_pt2; FindTarghet.Get_mezzo_rect_risc_pt3 = F_mezzo_rect_risc_pt3; FindTarghet.X_Point = F_intbarix; FindTarghet.Y_point = F_intbariy; FindTarghet.Angle = F_bariangle_template; FindTarghet.Major_Side1 = false; FindTarghet.Major_Side2 = false; FindTarghet.Get_Upper_Half_Square = false; FindTarghet.Get_Lower_Half_Square = false; return FindTarghet; } /***********MYQThread .cpp qthread::run*****************/ void datathread::run() { ... ... ... FindACPTarghet OneTarghet = FindCentPoint(rect_points, true, false); centrale = OneTarghet.Get_centrale; mezzo_rect_risc = OneTarghet.Get_mezzo_rect_risc; mezzo_rect_risc1 = OneTarghet.Get_mezzo_rect_risc1; mezzo_rect_risc_pt2 = OneTarghet.Get_mezzo_rect_risc_pt2; mezzo_rect_risc_pt3 = OneTarghet.Get_mezzo_rect_risc_pt3; intbarix = OneTarghet.X_Point; intbariy = OneTarghet.Y_point; bariangle = OneTarghet.Angle;
Not understand very well why I can't use return with struct ....
Regards
Giorgio -
But you can use struct as return. :)
FindTarghet.Get_centrale = F_centrale;
what is F_central?
I dont see you define it ?
so that would explain "undefined" -
@mrjj mmm....
/********* as global var in MYQThread .cpp*****************/ int F_intbarix = 0, F_intbariy = 0; double F_lung1 = 0, F_lung11 = 0, F_lung2 = 0, F_lung3 = 0, F_lung4 = 0, F_lung5 = 0, F_barix = 0, F_bariy = 0, F_bariangle = 0, F_bariangle_template = 0; int DeF_barix = 0, DeF_bariy = 0, DeF_bariangle = 0, DeF_bariangle_template = 0; int X_mezzo_rect_risc = 0, Y_mezzo_rect_risc = 0, X_mezzo_rect_risc1 = 0, Y_mezzo_rect_risc1 = 0; int X_mezzo_rect_risc_pt2 = 0, Y_mezzo_rect_risc_pt2 = 0, X_mezzo_rect_risc_pt3 = 0, Y_mezzo_rect_risc_pt3 = 0; int DeF_Major_Side1 = 0, DeF_Major_Side2 = 0, DeF_Get_Upper_Half_Square = 0, DeF_Get_Lower_Half_Square = 0; cv::Point F_centrale = cv::Point(0 ,0); <------------------------------------------------------- /****** and use these variable into calculation and as result too... so i put it into return struc as you see****/ cv::Point F_mezzo_rect_risc = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc1 = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc_pt2 = cv::Point(0 ,0);
Regards
giorgio -
@gfxx so I try to define F_centrale into struc func..
FindACPTarghet FindCentPoint( cv::Point2f Square_Points[4], bool Find_Upper_Half_Square, bool Find_Lower_Half_Square) { FindACPTarghet FindTarghet; <---------------------------------------- int F_intbarix = 0, F_intbariy = 0; double F_lung1 = 0, F_lung11 = 0, F_lung2 = 0, F_lung3 = 0, F_lung4 = 0, F_lung5 = 0, F_barix = 0, F_bariy = 0, F_bariangle = 0, F_bariangle_template = 0; int DeF_barix = 0, DeF_bariy = 0, DeF_bariangle = 0, DeF_bariangle_template = 0; int X_mezzo_rect_risc = 0, Y_mezzo_rect_risc = 0, X_mezzo_rect_risc1 = 0, Y_mezzo_rect_risc1 = 0; int X_mezzo_rect_risc_pt2 = 0, Y_mezzo_rect_risc_pt2 = 0, X_mezzo_rect_risc_pt3 = 0, Y_mezzo_rect_risc_pt3 = 0; int DeF_Major_Side1 = 0, DeF_Major_Side2 = 0, DeF_Get_Upper_Half_Square = 0, DeF_Get_Lower_Half_Square = 0; cv::Point F_centrale = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc1 = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc_pt2 = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc_pt3 = cv::Point(0 ,0); cv::Point F_un_quarto_rect_risc = cv::Point(0 ,0); cv::Point F_un_quarto_rect_risc1 = cv::Point(0 ,0); cv::Point F_un_quarto_rect_risc_pt2 = cv::Point(0 ,0); cv::Point F_un_quarto_rect_risc_pt3 = cv::Point(0 ,0); bool choose0 = false, choose1 = false, choose2 = false, choose3 = false; F_lung1 = std::pow((std::pow((Square_Points[0].x - Square_Points[1].x),2) + std::pow((Square_Points[0].y - Square_Points[1].y),2)), 0.5); F_lung2 = std::pow((std::pow((Square_Points[1].x - Square_Points[2].x),2) + std::pow((Square_Points[1].y - Square_Points[2].y),2)), 0.5); if (F_lung2 > F_lung1) {..................
but result is the same...
Regards
giorgio -
@gfxx Ok the function works whithout error ... but I have 0 result value for all variables ....
Regards
giorgio -
@gfxx mmm.... these type of approach is not desirable in my case ... so at the last I solve my problem wit a specific c++ class with function that return struct result ... if someone had this same problem or wish to create a class with a return statement struct or a void with a return struct placed here the basic steps ....
/*************************FILE.h*********************************/ #ifndef FILE_H #define FILE_H #include <QThread> /**** and other magic include ****/ #include <QtCore/QVariant> ..... struct myFirstStruct{ cv::Point2f Square_Points[4]; bool Up1_2Square; bool Down1_2Square; bool returnValue1; bool returnValue2; ...... /***other member DECLARE ***/ }; class File { public: File(); myFirstStruct myVoid( cv::Point2f Square_Points[4], bool Up1_2Square, bool Down1_2Square); struct myFirstStruct MyIstance; private: }; #endif // FILE_H /*************************FILE.cpp*********************************/ #include "File.h" File::File() { } myFirstStruct File::myVoid(cv::Point2f Square_Points[4], bool Up1_2Square, bool Down1_2Square) { /***do somethings*****/ MyIstance.returnValue1 = calculatedvalue1; MyIstance.returnValue2 = calculatedvalue2; return MyIstance; } /*************************InSomeFileOfYourApp.cpp*********************************/ #include "File.h" int var1; ..... Your::mainOrSub(){ File MyClassIstance; MyClassIstance.myVoid(cv-point-value, bool-value, bool-value); yourCalculatedData1 = myVoid.MyIstance.returnValue1; yourCalculatedData2 = myVoid.MyIstance.returnValue2; ....... }
I hope These Help someone ...
Regards
Giorgio -
@gfxx Class and Struct are the same thing.
struct MyClass{ // stuff }; // is the same as: class MyClass{ public: // stuff };
see http://www.cplusplus.com/doc/tutorial/classes/ for a quick overview of how to use classes and structs
-
@VRonin yes struct and class is quite similar .... but not the same things..... I know .. some C programmers not like class and prefer struct .... When I was A good programmer I use me too only struct and no class .... but for now I'm happy class exist .... ;)
regards
Giorgio -
@gfxx C has no classes. C has only structs and they cannot contain methods, just members. C is not an object oriented language. In C++ they are the same, the only difference is the default access level (public for struct, private for class)
-
@VRonin I Know that C has no classes .. I use it sometime, but I study it only 10 year ago and not use it for long long time ...... but I also know that many good programmers using C ++ but most use C do not really like the classes especially if you can use the struct .... but unfortunately I'm not a good programmer ... so use the classes and I facilitate my work .... anyway thanks to this link so today I can learn something new. :))
anyhow I'm not a good programmer and if you would suggest me some modification to my code example, in order to make me able to use struct instead class I'm happy to receive your suggest.
Some post ago I write these snip:
/***********MYQThread .h*****************/ struct FindACPTarghet{ cv::Point2f Square_Points[4]; bool Find_Upper_Half_Square; bool Find_Lower_Half_Square; cv::Point Get_centrale; cv::Point Get_mezzo_rect_risc; cv::Point Get_mezzo_rect_risc1; cv::Point Get_mezzo_rect_risc_pt2; cv::Point Get_mezzo_rect_risc_pt3; double X_Point; double Y_point; double Angle; bool Major_Side1; bool Major_Side2; bool Get_Upper_Half_Square; bool Get_Lower_Half_Square; //Get_Angle_Central_Point_Targhet GetDataTarghet; }/*FindCentPoint*/; class datathread : public QThread { Q_OBJECT public: explicit datathread(QObject *parent = 0, bool bthw = false); void run(); bool Stop; FindACPTarghet FindCentPoint( cv::Point2f Square_Points[4], bool Find_Upper_Half_Square, bool Find_Lower_Half_Square); /***********MYQThread .cpp*****************/ FindACPTarghet FindCentPoint( cv::Point2f Square_Points[4], bool Find_Upper_Half_Square, bool Find_Lower_Half_Square) { FindACPTarghet FindTarghet; <---------------------------------------- F_lung1 = 0; F_lung2 = 0; F_lung3 = 0; F_lung4 = 0; F_lung5 = 0; F_barix = 0; F_bariy = 0; F_intbarix = 0; F_intbariy = 0; F_bariangle = 0; bool choose0 = false, choose1 = false, choose2 = false, choose3 = false; F_lung1 = std::pow((std::pow((Square_Points[0].x - Square_Points[1].x),2) + std::pow((Square_Points[0].y - Square_Points[1].y),2)), 0.5); F_lung2 = std::pow((std::pow((Square_Points[1].x - Square_Points[2].x),2) + std::pow((Square_Points[1].y - Square_Points[2].y),2)), 0.5); if (F_lung2 > F_lung1) {.................. ........................ FindTarghet.Get_centrale = F_centrale; <--------------------------------- /*******************************undefined error reference**********************/ FindTarghet.Get_mezzo_rect_risc = F_mezzo_rect_risc; FindTarghet.Get_mezzo_rect_risc1 = F_mezzo_rect_risc1; FindTarghet.Get_mezzo_rect_risc_pt2 = F_mezzo_rect_risc_pt2; FindTarghet.Get_mezzo_rect_risc_pt3 = F_mezzo_rect_risc_pt3; FindTarghet.X_Point = F_intbarix; FindTarghet.Y_point = F_intbariy; FindTarghet.Angle = F_bariangle_template; FindTarghet.Major_Side1 = false; FindTarghet.Major_Side2 = false; FindTarghet.Get_Upper_Half_Square = false; FindTarghet.Get_Lower_Half_Square = false; return FindTarghet; } /***********MYQThread .cpp qthread::run*****************/ void datathread::run() { ... ... ... FindACPTarghet OneTarghet = FindCentPoint(rect_points, true, false); centrale = OneTarghet.Get_centrale; mezzo_rect_risc = OneTarghet.Get_mezzo_rect_risc; mezzo_rect_risc1 = OneTarghet.Get_mezzo_rect_risc1; mezzo_rect_risc_pt2 = OneTarghet.Get_mezzo_rect_risc_pt2; mezzo_rect_risc_pt3 = OneTarghet.Get_mezzo_rect_risc_pt3; intbarix = OneTarghet.X_Point; intbariy = OneTarghet.Y_point; bariangle = OneTarghet.Angle;
Not work ... but compile is ok...
Thank a lot
Giorgio