Download MetaTrader 5

instantiate objects from an array

To add comments, please log in or register
marksimmonds
18
marksimmonds  

Hi,

I'm trying to create objects by looping through an array...... here's the code, but when I try to compile, I get the error "  '[' - invalid index error  "

Can somebody confirm if it can be done, and what I'm doing wrong

Thanks in advance.

//---Create Array of Symbols we wish to interogate
string Instruments[30] = {"AUDCAD", "AUDCHF", "AUDJPY", "AUDNZD", "AUDUSD", "CADCHF", "CADJPY", "CHFJPY",
                      "EURAUD", "EURCAD", "EURCHF", "EURGBP", "EURJPY", "EURNZD", "EURTRY", "EURUSD",
                      "GBPAUD", "GBPCAD", "GBPCHF", "GBPJPY", "GBPNZD", "GBPUSD", "NZDCAD", "NZDCHF",
                      "NZDJPY", "NZDUSD", "USDCAD", "USDCHF", "USDJPY", "USDTRY"};
                      
//---CREATE CLASS STRUCTURE FOR SIGNALS

class PriceAction
  {
public:
   string name;
 };

for(int i=0; i < ArraySize(Instruments); i++)
     {
      PriceAction Instruments[i]();
      //Alert("New Object created:", Symbols[i].name);
     }
Ex Ovo Omnia
3263
Ex Ovo Omnia  
Did you noticed you have Instruments declared twice? The second declaration is invalid, and moreover its live cycle is only within the current block.
whroeder1
15335
whroeder1  
  1. Ovo: Did you noticed you have Instruments declared twice? The second declaration is invalid, and moreover its live cycle is only within the current block.
    If you had used #property strict, the compiler would have warned you about that.

  2. To create an array of objects the object must have a default constructor. See Structures and Classes - MQL4 Documentation. This you have since you didn't declare any constructors.
  3. You must do any other initialization in a method.

To add comments, please log in or register