Structures or classes containing objects are not allowed

 

Hello,

I am newbie here, so sorry if something wrong with this post.

I am working on loading csv structure to array and I am getting this error Structures or classes containing objects are not allowed.

I found https://www.mql5.com/en/forum/255091 but not helped me with this error.

CSV file consists this lines:

80f88196527c4ba19426460f919668dc,2020.05.22 10:00:00,EURCAD,BUY,1.52100,1.52200,1.52300,1.52400,1.51000
c264e4ca619c4696b9abe49a86956a6d,2020.05.21 10:00:00,CADJPY,BUY,77.400,77.500,77.600,78.700,76.000

Code for reading to structure:

#define MC_MAX 15

struct signals_data{
   string order_id;
   datetime actual_date;
   string order_date;   
   string symbol;       
   string operation;   
   double price;       
   double tp_1;         
   double tp_2;         
   double tp_3;         
   double sl;           
};

void OnStart(){
//---
   string export_path = "export.csv";
   //---
   string       order_id[MC_MAX];
   datetime     actual_date[MC_MAX];
   string       order_date[MC_MAX];   
   string       symbol[MC_MAX];       
   string       operation[MC_MAX];   
   double       price[MC_MAX];       
   double       tp_1[MC_MAX];         
   double       tp_2[MC_MAX];         
   double       tp_3[MC_MAX];         
   double       sl[MC_MAX]; 
   signals_data signals_arr[MC_MAX];
   //---
   ResetLastError();
   int export_file_handle = FileOpen(export_path, FILE_READ, ",");
   if(export_file_handle != INVALID_HANDLE){
      if(!FileReadStruct(export_file_handle, signals_arr[0])){
         PrintFormat("Error reading data. Error code=%d", GetLastError()); 
      }
      Print("handle: ", export_file_handle," : ", signals_arr[export_file_handle].order_id);
      FileFlush(export_file_handle);
      FileClose(export_file_handle);
   }else{
       Print("File open failed, error ", GetLastError());
   }
}

And after compile, the error is made.

I was also trying to change

FileReadStruc -> FileReadArray
FileOpen(export_path, FILE_READ|FILE_CSV, ",")

But not helped me.

Thank you for your help.

Have a nice day.



MT4-Read a CSV file and load the info in a struct
MT4-Read a CSV file and load the info in a struct
  • 2018.06.11
  • www.mql5.com
I need to read a CSV file like the lines bellow and load in the a structure to access bellow...
 
  1. yolocalhost: I am working on loading csv structure to array and I am getting this error Structures or classes containing objects are not allowed.

    struct signals_data{
       string order_id;
    ⋮
       signals_data signals_arr[MC_MAX];
    In order to create the elements in the array, the compiler needs to know how to initialize the class/struct. It doesn't know how, because of the strings. Add a default constructor (inside the struct): void signal_data(void) : order_id(""), order_date(""), symbol(""), operation(""){}
    The default constructor has a special purpose when initializing an array of objects of its class
              Structures, Classes and Interfaces - Data Types - Language Basics - MQL4 Reference

    Why are those (except symbol) strings? You should change the types to actual values and adjust the read method (№ 3)

  2.       if(!FileReadStruct(export_file_handle, signals_arr[0])){
    Your file is text. Read Struct only works with binary files; you have text. Read the manual! Add a read method to your struct, so it reads itself:
    void read(int H){
       order_id     = FileReadString(H);
       actual_date  = FileReadDateTime(H);
       symbol       = FileReadString(H);
       operation    = FileReadString(H);
       price        = FileReadNumber(H);       
    ⋮
  3. yolocalhost: I was also trying to change
    FileReadStruc -> FileReadArray
    FileOpen(export_path, FILE_READ|FILE_CSV, ",")
    Read Array only works with binary files; you have text. Read the manual! Open the file, then while it is not ending, have the next array element read it: for(int i=0; !FileIsEnding(export_file_handle); ++i) signals_arr[i].read(export_file_handle);

  4. You are reading, what are you attempting to flush?

  5.    string       order_id[MC_MAX];
       datetime     actual_date[MC_MAX];
       string       order_date[MC_MAX];   
       string       symbol[MC_MAX];       
       string       operation[MC_MAX];   
       double       price[MC_MAX];       
       double       tp_1[MC_MAX];         
       double       tp_2[MC_MAX];         
       double       tp_3[MC_MAX];         
       double       sl[MC_MAX]; 
    Drop unused declarations.
 

Thank you William, you are helpful.

Here is working code:

#define MC_MAX 15

struct signals_data{
   string order_id;
   string order_date;   
   string symbol;       
   string operation;   
   double price;       
   double tp_1;         
   double tp_2;         
   double tp_3;         
   double sl;   
   //--- Default constructor
   signals_data() { order_id=""; order_date=""; symbol=""; operation=""; price=0.0; tp_1=0.0; tp_2=0.0; tp_3=0.0; sl=0.0; }
   //--- Read data
   void read(int handler){
      order_id     = FileReadString(handler);
      order_date   = FileReadString(handler);
      symbol       = FileReadString(handler);
      operation    = FileReadString(handler);
      price        = FileReadNumber(handler);
      tp_1         = FileReadNumber(handler);
      tp_2         = FileReadNumber(handler);
      tp_3         = FileReadNumber(handler);
      sl           = FileReadNumber(handler);
   }
};
   
void OnStart(){
//---
   string export_path = "export.csv";
   //---
   signals_data signals_arr[MC_MAX];
   //---
   ResetLastError();
   int export_file_handle = FileOpen(export_path, FILE_READ, ",");
   if(export_file_handle != INVALID_HANDLE){
      for(int i=0; !FileIsEnding(export_file_handle); i++){
         signals_arr[i].read(export_file_handle);
      }
      Print(signals_arr[0].order_id);
      Print(signals_arr[0].order_date);
      
      FileClose(export_file_handle);
   }else{
       Print("File open failed, error ", GetLastError());
   }
}