Create an array of structs

Back to topics list To post a new topic, please log in or register
avatar
14
jt27 2016.07.13 18:44
 

I am trying to create an array of structs, everything I find online indicates there shouldn't be a problem with that, but I can't seem to get my code to compile when I try to do this.  Here is what I am trying to do:

// Heiken Ashi Candle Struct
int ha = 20;   // Make this number at least 5 more than the number of candles you plan on analyzing
struct heikenAshi {
   double haClose;
   double haOpen;
   double haHigh;
   double haLow;
}; 

struct heikenAshi haCandles[ha];  // Array of 'ha' Heiken Ashi candles with corresponding properties

 

 Any suggestions as to what I'm doing wrong?

avatar
1111
Ovo 2016.07.13 19:56 #
 

I would modify it like that:

 // Heiken Ashi Candle Struct

#define ha 20   // Make this number at least 5 more than the number of candles you plan on analyzing
struct heikenAshi {
   double haClose;
   double haOpen;
   double haHigh;
   double haLow;
   heikenAshi() {
	haClose = 0.0;haOpen = 0.0;haHigh = 0.0;haLow = 0.0;
   }
}; 

heikenAshi haCandles[ha];  // Array of 'ha' Heiken Ashi candles with corresponding properties

 

 

 

 

avatar
14
jt27 2016.07.14 02:24 #
 
Ovo:

I would modify it like that:

 // Heiken Ashi Candle Struct

 

 

 

 

Thank you for your help!  If you don't mind, could you explain the purpose of putting the heikenAshi()... member in the struct?  I'm a hardware engineer and know just enough programming to get myself in trouble!
avatar
1111
Ovo 2016.07.14 10:20 #
 
jt27:
Thank you for your help!  If you don't mind, could you explain the purpose of putting the heikenAshi()... member in the struct?  I'm a hardware engineer and know just enough programming to get myself in trouble!
It is a constructor (a method which is triggered after the structure allocation), and in this case it sets default values to the struct members. Otherwise the values would be undefined (not initialised).
Back to topics list  

To add comments, please log in or register