Why does this class method call in mql5 give an unexpected token error on compilation?
Why does this class method call in mql5 give an unexpected token error on compilation?
Loading saved threads...
Andy Thompson · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Andy Thompson
Original post: https://stackoverflow.com/questions/76611611
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
When I try to compile the following code:
struct pricePoint{
double price;
datetime time;
public:
pricePoint(){
price = 0;
time = 0;
}
};
class CMktStruct{
pricePoint h[2];
pricePoint l[2];
public:
CMktStruct(void);
void PrintStruct(){PrintFormat("HiPrice1=%.5f HiTime1=%i; HiPrice2=%.5f HiTime2=%i: LoPrice1=%.5f LoTime1=%i; LoPrice2=%.5f LoTime2=%i",h[0].price,h[0].time,h[1].price,h[1].time,l[0].price,l[0].time,l[1].price,l[1].time);};
};
CMktStruct::CMktStruct(void){
l[0].price = DBL_MAX;
l[1].price = DBL_MAX;
}
CMktStruct ms();
ms.PrintStruct();
I get an
'ms' - unexpected token, probably type is missing?
error.
Commenting out the last line leads to the code compiling with no problem.
I want to print the Market Structure as defined (after initialisation and without further modification this should give "HiPrice1=0.00000 HiTime1=0; HiPrice2=0.00000 HiTime2=0: LoPrice1=1.79769e+308 LoTime1=0; LoPrice2=1.79769e+308 LoTime2=0")
Quote
Report
LuizABL · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: LuizABL
Original post: https://stackoverflow.com/a/76972409
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You cant execute a method in global scope...
Try this:
struct pricePoint{
double price;
datetime time;
public:
pricePoint(){
price = 0;
time = 0;
}
};
class CMktStruct{
pricePoint h[2];
pricePoint l[2];
public:
CMktStruct(void);
void PrintStruct(){PrintFormat("HiPrice1=%.5f HiTime1=%i; HiPrice2=%.5f HiTime2=%i: LoPrice1=%.5f LoTime1=%i; LoPrice2=%.5f LoTime2=%i",h[0].price,h[0].time,h[1].price,h[1].time,l[0].price,l[0].time,l[1].price,l[1].time);};
};
CMktStruct::CMktStruct(void){
l[0].price = DBL_MAX;
l[1].price = DBL_MAX;
}
CMktStruct ms();
int OnInit()
{
// Executing the method inside localscope
ms.PrintStruct();
return(INIT_SUCCEEDED);
}
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: LuizABL Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/76972409 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. You cant execute a method in global scope... Try this: struct pricePoint{ double price; datetime time; public: pricePoint(){ price = 0; time = 0; } }; class CMktStruct{ pricePoint h[2]; pricePoint l[2]; public: CMktStruct(void); void PrintStruct(){PrintFormat("HiPrice1=%.5f HiTime1=%i; HiPrice2=%.5f HiTime2=%i: LoPrice1=%.5f LoTime1=%i; LoPrice2=%.5f LoTime2=%i",h[0].price,h[0].time,h[1].price,h[1].time,l[0].price,l[0].time,l[1].price,l[1].time);}; }; CMktStruct::CMktStruct(void){ l[0].price = DBL_MAX; l[1].price = DBL_MAX; } CMktStruct ms(); int OnInit() { // Executing the method inside localscope ms.PrintStruct(); return(INIT_SUCCEEDED); }
Checking account access…