Download MetaTrader 5

String Arrays in a structure

To add comments, please log in or register
Paul Anscombe
4181
Paul Anscombe  

I am trying to populate a string array inside a structure but the compiler errors every time (see "string Label")

struct PIVOT_Structure
{
   datetime TimeStart;
   datetime TimeEnd;
   double   PP,S1,S2,S3,R1,R2,R3;   
   double   Line[7];
   string   Label[7] = {"HR-PP","HR-R1","HR-R2","HR-R3","HR-S1","HR-S1","HR-S1"}; 
};

if you place the  line outside of the structure then it works fine.

Is there a reason that you cannot initialize the value of the string array inside the structure? It is a pain because it means manually coding the loading of each string value. (I have many of these to do)

Alain Verleyen
Moderator
32026
Alain Verleyen  

If I understood well, you need to have the same value for all your variable of PIVOT_Structure type.

So you have to use a static :

struct PIVOT_Structure
  {
   datetime          TimeStart;
   datetime          TimeEnd;
   double            PP,S1,S2,S3,R1,R2,R3;
   double            Line[7];
   static string     Label[];
  };
string PIVOT_Structure::Label[]={"HR-PP","HR-R1","HR-R2","HR-R3","HR-S1","HR-S1","HR-S1"};

So you have a static and dynamic array :-D.

Paul Anscombe
4181
Paul Anscombe  
angevoyageur:

If I understood well, you need to have the same value for all your variable of PIVOT_Structure type.

So you have to use a static :

So you have a static and dynamic array :-D.

excellent thanks for that :-D
amin_mohammadi
88
amin_mohammadi  

I want to initialize an array in a class. the array is in string type. the compiler error is this:

{ - expression expected - The underlined and bolded line of code

And the code is as below

//+------------------------------------------------------------------+
//|                                              CurrencySymbols.mqh |
//|                                                   Amin Mohammadi |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Amin Mohammadi"
#property link      "http://www.mql5.com"
#property version   "1.00"


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

struct Currencies
{
static string Major[];
};

class CurrencySymbols
  {
  protected:
  Currencies currency;


private:
 
  void SetCurrencyValue();
 void GetCurrencyValue();
 void SetCurrencySymbols();
 void GetCurrencySymbols();
public:


                     CurrencySymbols();
                     
                    ~CurrencySymbols();
                    

  };
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CurrencySymbols::CurrencySymbols()
  { 
  
  currency.Major[8]={"AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NZD", "USD"}; 
  
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
CurrencySymbols::~CurrencySymbols()
  {
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// Get currencies symbol
//+------------------------------------------------------------------+
CurrencySymbols::GetCurrencySymbols()
{

} 
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// Set currencies symbol
//+------------------------------------------------------------------+
CurrencySymbols::SetCurrencySymbols()
{

} 
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// Get currencies Value
//+------------------------------------------------------------------+
CurrencySymbols::GetCurrencyValue()
{
}
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
// Set currencies value
//+------------------------------------------------------------------+
CurrencySymbols::SetCurrencyValue()
{
}                    
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
32026
Alain Verleyen  
amin_mohammadi:

I want to initialize an array in a class. the array is in string type. the compiler error is this:

{ - expression expected - The underlined and bolded line of code

And the code is as below

You need to initialize your array outside your class :

string Currencies::Major[]={"AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NZD", "USD"}; 

Why do you need a structure and a class ?

amin_mohammadi
88
amin_mohammadi  
angevoyageur:

You need to initialize your array outside your class :

Why do you need a structure and a class ?

Sorry for delay in response,

I need a public static array to save a double value for each currency. I don't know what is the standard way to do that in class.

When I try to put the definition inside the class I get this error.

To add comments, please log in or register