-
Sorry, I have not yet mastered the C ++ and even the use of struct ... so I tried to do that in order to optimize the code ... I have to find in many function of my Qthread the same values type (using the same calculation and parameter) but from different images. ...
the wrong code:
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; cv::Point F_centrale = cv::Point(0 ,0); cv::Point F_mezzo_rect_risc; cv::Point F_mezzo_rect_risc1; cv::Point F_mezzo_rect_risc_pt2; cv::Point F_mezzo_rect_risc_pt3; cv::Point F_un_quarto_rect_risc; cv::Point F_un_quarto_rect_risc1; cv::Point F_un_quarto_rect_risc_pt2; cv::Point F_un_quarto_rect_risc_pt3; struct Find_Angle_Central_Point_Targhet{ cv::Point2f Square_Points[4]; bool Find_Upper_Half_Square; bool Find_Lower_Half_Square; }; struct Get_Angle_Central_Point_Targhet{ cv::Point Get_cent; cv::Point Get_rect_risc; cv::Point Get_rect_risc1; cv::Point Get_rect_risc_pt2; cv::Point Get_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; }; class datathread : public QThread { Q_OBJECT public: explicit datathread(QObject *parent = 0, bool bthw = false); void run(); bool Stop; Find_Angle_Central_Point_Targhet Find_A_Cent_P( cv::Point2f Square_Points[4], bool Find_Upper_Half_Square, bool Find_Lower_Half_Square) { 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; 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(Find_Upper_Half_Square) { F_mezzo_rect_risc = cv::Point(round((Square_Points[0].x + Square_Points[1].x)/2), round((Square_Points[0].y + Square_Points[1].y)/2)); F_mezzo_rect_risc1 = cv::Point(round((Square_Points[2].x + Square_Points[3].x)/2), round((Square_Points[2].y + Square_Points[3].y)/2)); F_mezzo_rect_risc_pt2 = cv::Point(Square_Points[2].x, Square_Points[2].y); F_mezzo_rect_risc_pt3 = cv::Point(Square_Points[1].x, Square_Points[1].y); } else if (Find_Lower_Half_Square) { Get_Angle_Central_Point_Targhet Get_A_Cent_P; Get_A_Cent_P.Get_centrale = F_centrale; Get_A_Cent_P.Get_mezzo_rect_risc = F_mezzo_rect_risc; Get_A_Cent_P.Get_mezzo_rect_risc1 = F_mezzo_rect_risc1; Get_A_Cent_P.Get_mezzo_rect_risc_pt2 = F_mezzo_rect_risc_pt2; Get_A_Cent_P.Get_mezzo_rect_risc_pt3 = F_mezzo_rect_risc_pt3; Get_A_Cent_P.X_Point = F_intbarix; Get_A_Cent_P.Y_point = F_intbariy; Get_A_Cent_P.Angle = F_bariangle_template; Get_A_Cent_P.Major_Side1 = false; Get_A_Cent_P.Major_Side2 = false; Get_A_Cent_P.Get_Upper_Half_Square = false; Get_A_Cent_P.Get_Lower_Half_Square = false; return Get_A_Cent_P; }
the error is :
datathread.h:222: error: could not convert 'Get_A_Cent_P' from 'Get_Angle_Central_Point_Targhet' to 'Find_Angle_Central_Point_Targhet' return Get_A_Cent_P; ^
I have try to put my center-find function on datathread.h and my intention is to call it more time in datathread.cpp .... Is my strategy wrong or not permitted in C++??
Regards
Giorgio -
Hi
A struct is a type
and it will not just convert between them.Your naming of the structs is very verbose and hard to read & understand as you make
the type look like function name.it seems you define
Find_Angle_Central_Point_Targhet Find_A_Cent_P(xx)so return type is Find_Angle_Central_Point_Targhet
and in the function you define
Get_Angle_Central_Point_Targhet Get_A_Cent_P;
return Get_A_Cent_P; << this is not correct type to return. -
@mrjj ok I suspect these ... I understand not possible return other struct type .... but I can define these struct and use these struct func?:
struct a{ int x; double y; cv::Point cerr0; cv::Point cerr1; cv::Point cerr2; cv::Point cerr3; } ........ a afunc (cv::Point cerr0){ /* <---------- no pass all value of a struct only one .... but return all*/ /*my operation .....*/ afunc.x = some_value0; afunc.y = some_value1; afunc.cerr0 = some_value0; afunc.cerr1 = some_value1; afunc.cerr2 = some_value2; afunc.cerr3 = some_value3; return afunc; } These is valid use of struct func?? regards Giorgio
-
Yes, thats correct but its a type so it wont convert.
so if you say
struct A;
struct B;A myfunc() {
A mine;
return mine; // ok correct type
}A myfunc() {
B mine;
return mine; // error B is not A type
} -
@gfxx said in C++ struct function with return struct ...:
a afunc (cv::Point cerr0){ /* <---------- no pass all value of a struct only one .... but return all*/
/my operation ...../
afunc.x = some_value0;
afunc.y = some_value1;
afunc.cerr0 = some_value0;
afunc.cerr1 = some_value1;
afunc.cerr2 = some_value2;
afunc.cerr3 = some_value3;return afunc;
}This does not make any sense. You probably mean:
a afunc (cv::Point cerr0){ /*my operation .....*/ a result; result.x = some_value0; result.y = some_value1; result.cerr0 = some_value0; result.cerr1 = some_value1; result.cerr2 = some_value2; result.cerr3 = some_value3; return result; }
-
@jsulm said in C++ struct function with return struct ...:
@gfxx said in C++ struct function with return struct ...:
a afunc (cv::Point cerr0){ /* <---------- no pass all value of a struct only one .... but return all*/
/my operation ...../
afunc.x = some_value0;
afunc.y = some_value1;
afunc.cerr0 = some_value0;
afunc.cerr1 = some_value1;
afunc.cerr2 = some_value2;
afunc.cerr3 = some_value3;return afunc;
}This does not make any sense. You probably mean:
a afunc (cv::Point cerr0){ /*my operation .....*/ a result; result.x = some_value0; result.y = some_value1; result.cerr0 = some_value0; result.cerr1 = some_value1; result.cerr2 = some_value2; result.cerr3 = some_value3; return result; }
You are perfectly in right .... but anyhow the function is write in my file.h and I would reuse it in file.ccp ... but if in file.cpp i write:
afunc (cv::Point myvalue); /* I expect to be able to write these:*/ myvar1 = result.x; myvar2 = result.y; etc. etc.... /*but these is not possible .... or qt not permit it*/
Regards
Giorgio -
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 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 -
@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