求教complex复数结构编译错误

 

求教一下MQL5帮助文档的 complex 例子代码(MT5)编译错误,在使用ALGLIB数学库编译UseAlglib.mt5也是同样问题,但 UseAlglib.mt4能编译通过

//+------------------------------------------------------------------+

//|                                                         TEST.mq5 |
//|                                   Copyright 2021, DaYingCao Ltd. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, DaYingCao Ltd."
#property link      "https://www.mql5.com/en/users/cdymql4/seller#products"
#property version   "1.00"


//+------------------------------------------------------------------+ 
//| A structure for operations with complex numbers                  | 
//+------------------------------------------------------------------+ 
struct complex 
  { 
   double            re; // Real part 
   double            im; // Imaginary part 
   //--- Constructors 
                     complex():re(0.0),im(0.0) {  } 
                     complex(const double r):re(r),im(0.0) {  } 
                     complex(const double r,const double i):re(r),im(i) {  } 
                     complex(const complex &o):re(o.re),im(o.im) { } 
   //--- Arithmetic operations 
   complex           Add(const complex &l,const complex &r) const;  // Addition 
   complex           Sub(const complex &l,const complex &r) const;  // Subtraction 
   complex           Mul(const complex &l,const complex &r) const;  // Multiplication 
   complex           Div(const complex &l,const complex &r) const;  // Division 
  };
 
void OnStart() 
  { 
//--- Declare and initialize variables of a complex type 
   complex a(2,4),b(-4,-2); 
   PrintFormat("a=%.2f+i*%.2f,   b=%.2f+i*%.2f",a.re,a.im,b.re,b.im); 
//--- Sum up two numbers 
   complex z; 
   z=a.Add(a,b); 
   PrintFormat("a+b=%.2f+i*%.2f",z.re,z.im); 
//--- Multiply two numbers 
   z=a.Mul(a,b); 
   PrintFormat("a*b=%.2f+i*%.2f",z.re,z.im); 
//--- Divide two numbers 
   z=a.Div(a,b); 
   PrintFormat("a/b=%.2f+i*%.2f",z.re,z.im); 
//--- 

  }

编译错误:

'TEST.mq5' TEST.mq5 1 1

'complex' - unexpected token TEST.mq5 14 8

'{' - name expected TEST.mq5 15 3

're' - undeclared identifier TEST.mq5 34 51

'im' - undeclared identifier TEST.mq5 34 56

're' - undeclared identifier TEST.mq5 34 61

'im' - undeclared identifier TEST.mq5 34 66

'Add' - undeclared identifier TEST.mq5 37 8

',' - unexpected token TEST.mq5 37 13

'a' - some operator expected TEST.mq5 37 12

')' - unexpected token TEST.mq5 37 15

're' - undeclared identifier TEST.mq5 38 36

'im' - undeclared identifier TEST.mq5 38 41

'Mul' - undeclared identifier TEST.mq5 40 8

',' - unexpected token TEST.mq5 40 13

'a' - some operator expected TEST.mq5 40 12

')' - unexpected token TEST.mq5 40 15

're' - undeclared identifier TEST.mq5 41 36

'im' - undeclared identifier TEST.mq5 41 41

'Div' - undeclared identifier TEST.mq5 43 8

',' - unexpected token TEST.mq5 43 13

'a' - some operator expected TEST.mq5 43 12

')' - unexpected token TEST.mq5 43 15

're' - undeclared identifier TEST.mq5 44 36

'im' - undeclared identifier TEST.mq5 44 41

24 errors, 0 warnings 25 1




Daying Cao
Daying Cao
  • www.mql5.com
Trader's profile
 
Daying Cao:

求教一下MQL5帮助文档的 complex 例子代码(MT5)编译错误,在使用ALGLIB数学库编译UseAlglib.mt5也是同样问题,但 UseAlglib.mt4能编译通过

//+------------------------------------------------------------------+

//|                                                         TEST.mq5 |
//|                                   Copyright 2021, DaYingCao Ltd. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, DaYingCao Ltd."
#property link      "https://www.mql5.com/en/users/cdymql4/seller#products"
#property version   "1.00"


//+------------------------------------------------------------------+ 
//| A structure for operations with complex numbers                  | 
//+------------------------------------------------------------------+ 
struct complex 
  { 
   double            re; // Real part 
   double            im; // Imaginary part 
   //--- Constructors 
                     complex():re(0.0),im(0.0) {  } 
                     complex(const double r):re(r),im(0.0) {  } 
                     complex(const double r,const double i):re(r),im(i) {  } 
                     complex(const complex &o):re(o.re),im(o.im) { } 
   //--- Arithmetic operations 
   complex           Add(const complex &l,const complex &r) const;  // Addition 
   complex           Sub(const complex &l,const complex &r) const;  // Subtraction 
   complex           Mul(const complex &l,const complex &r) const;  // Multiplication 
   complex           Div(const complex &l,const complex &r) const;  // Division 
  };
 
void OnStart() 
  { 
//--- Declare and initialize variables of a complex type 
   complex a(2,4),b(-4,-2); 
   PrintFormat("a=%.2f+i*%.2f,   b=%.2f+i*%.2f",a.re,a.im,b.re,b.im); 
//--- Sum up two numbers 
   complex z; 
   z=a.Add(a,b); 
   PrintFormat("a+b=%.2f+i*%.2f",z.re,z.im); 
//--- Multiply two numbers 
   z=a.Mul(a,b); 
   PrintFormat("a*b=%.2f+i*%.2f",z.re,z.im); 
//--- Divide two numbers 
   z=a.Div(a,b); 
   PrintFormat("a/b=%.2f+i*%.2f",z.re,z.im); 
//--- 

  }

编译错误:

'TEST.mq5' TEST.mq5 1 1

'complex' - unexpected token TEST.mq5 14 8

'{' - name expected TEST.mq5 15 3

're' - undeclared identifier TEST.mq5 34 51

'im' - undeclared identifier TEST.mq5 34 56

're' - undeclared identifier TEST.mq5 34 61

'im' - undeclared identifier TEST.mq5 34 66

'Add' - undeclared identifier TEST.mq5 37 8

',' - unexpected token TEST.mq5 37 13

'a' - some operator expected TEST.mq5 37 12

')' - unexpected token TEST.mq5 37 15

're' - undeclared identifier TEST.mq5 38 36

'im' - undeclared identifier TEST.mq5 38 41

'Mul' - undeclared identifier TEST.mq5 40 8

',' - unexpected token TEST.mq5 40 13

'a' - some operator expected TEST.mq5 40 12

')' - unexpected token TEST.mq5 40 15

're' - undeclared identifier TEST.mq5 41 36

'im' - undeclared identifier TEST.mq5 41 41

'Div' - undeclared identifier TEST.mq5 43 8

',' - unexpected token TEST.mq5 43 13

'a' - some operator expected TEST.mq5 43 12

')' - unexpected token TEST.mq5 43 15

're' - undeclared identifier TEST.mq5 44 36

'im' - undeclared identifier TEST.mq5 44 41

24 errors, 0 warnings 25 1




你就不能用插入代码方式提问吗?


这是MT5给我们提供的一个隐式结构,这是为了方便我的在操作结构时难免会有一些加减的运算需求。一般来说系统是不直接提供结构的运算操作的,无论C还是C++也好,都没这种机制。只能靠我们自己根据需求去重载。


但在MT5中系统给我重载了一个complex隐式结构,可提供一般的运算,如加减乘除,你要不好理解,你直接把它当个函数用就是了。


再简单点理解就是MQL5提供了一个可以进行运算的隐式结构体,这样我们直接用就行了,没必要再去构造一个,所以complex在MT5中是一个关键词,你是编译不过的,因为这有命名冲突。


之所以你在MT4中能编译通过,那是因为MT4中系统并没有给提供这个隐藏结构,所以这个complex在MT4中并不是关键词,不存在命名冲突,你当然能编译通过,但是在MT5中就会报错。


一句话说,就是我们想进行结构之间进行一些简单的运算,不用去构造重载,直接用就是了。但在MT4中系统没提供,我们得去构造一个。


文中系统给的案例说明,并不让我们去这样做一次,而是告诉我们系统是如何去重载这个结构的,但系统把它隐藏了 ,我们看不到,我们只能直接用,所以并不是让我们去复制粘贴到MT5中编译的。


我尽力了,如果还是不明白,另请高明 。

原因: