MQL4将对象数组作为函数参数发送

问题描述 投票:0回答:1

我具有作为类成员的功能,并将我的自定义对象的数组作为参数:

class Stochastic { ... some class which sent into initializeStochastics method as param };

class StochasticInitializer {
    public:
        Properties *properties[8];

    public:
        StochasticInitializer(void) {
           this.properties = ... 
        }

    public:
        void initializeStochastics(Stochastic& *stochastics[]) { // This param is my problem
            for (int i = 0 ;i < ArraySize(properties); i++) {
                if (properties[i].enabled) {
                    stochastics[i] = new Stochastic(properties[i]);
                }
            }
        }
};

我的错误:

'&' - comma expected
']' - declaration without type
']' - comma expected
'initializeStochastics' - wrong parameters count
'stochastics' - undeclared identifier

我从here中获取语法,但也许是MQL5的解决方案。

我可以将类实例的数组作为MQL4中的方法参数发送吗?如果为“是”,则为-,如果为否,则也为-。

mql4
1个回答
0
投票

一切正常(几乎可行)只是决定是要创建全局数组还是具有指针访问权限(需要在完成后删除它)。这是指针的示例。另外,请下次提供MCVE,因为有人需要编写所有无用的东西(例如properties&stoch类)以使其可测试。

class Properties
  {
public:
   bool  enabled;
   int   periodK;
   Properties(bool _enabled,int k):enabled(_enabled),periodK(k){}
  ~Properties(){}
  };
class Stochastic
  {
public:
   int   periodK;

   Stochastic(){}
  ~Stochastic(){}
   Stochastic(Properties *prop):periodK(prop.periodK){}

   double get(const int shift,const int buffer=0)const{return iStochastic(_Symbol,0,periodK,3,3,MODE_SMA,STO_LOWHIGH,buffer,shift);}
  };
class StochasticInitializer
  {
public:
   Properties *properties[8];

   StochasticInitializer()
     {
      Deinit();
      properties[0]=new Properties(true,5);
      properties[1]=new Properties(true,13);
      properties[2]=new Properties(true,14);
      properties[3]=new Properties(true,15);
      properties[4]=new Properties(true,16);
      properties[5]=new Properties(true,17);
      properties[6]=new Properties(true,18);
      properties[7]=new Properties(false,19);
     }
  ~StochasticInitializer(){Deinit();}
   void        Deinit(const int reason=0){   for(int i=0;i<ArraySize(properties);i++)delete(properties[i]);   }
   void        initializeStochastics(Stochastic *&stochastics[])// THIS IS WHAT YOU NEED IN CASE OF POINTERS
     {
      for(int i=0;i<ArraySize(properties);i++)
        {
         if(properties[i].enabled)
           {
            stochastics[i]=new Stochastic(properties[i]);
           }
        }
     }
  };
StochasticInitializer initializer;
void OnTick()
    {
       Stochastic *array[8];    //THIS IS ARRAY OF POINTERS
       initializer.initializeStochastics(array);
       for(int i=0;i<ArraySize(array);i++)
         {
          printf("%i %s: %d %s",__LINE__,__FILE__,i,CheckPointer(array[i])==POINTER_INVALID ? "null" : (string)array[i].periodK);
         }
       for(int i=ArraySize(array)-1;i>=0;i--)delete(array[i]);//DELETING POINTERS
       ExpertRemove();
    }
© www.soinside.com 2019 - 2024. All rights reserved.