【发布时间】:2015-06-17 15:46:03
【问题描述】:
以下MQL4代码出现错误:
#property version "1.00"
#property strict
struct prices
{
datetime date; // date
double bid; // bid price
double ask; // ask price
};
prices arr[];
string path ;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
path = "script.log";
//--- save data to array
arr[0].date = TimeCurrent();
arr[0].bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
arr[0].ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
//--- show current data
Print("Date = ",arr[0].date," Bid = ",arr[0].bid," Ask = ",arr[0].ask);
//--- increase the counter
//--- if the array is filled, write data to the file and zero it out
WriteData(1);
}
//+------------------------------------------------------------------+
//| Write n elements of the array to file |
//+------------------------------------------------------------------+
void WriteData(const int n)
{
//--- open the file
ResetLastError();
int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
if(handle!=INVALID_HANDLE)
{
//--- write array data to the end of the file
FileSeek(handle,0,SEEK_END);
FileWriteArray(handle,arr,0,n);
//--- close the file
FileClose(handle);
}
else
Print("Failed to open the file, error ",GetLastError());
}
//+------------------------------------------------------------------+
错误是:
2015.06.17 15:39:25.561 array out of range in 'Struct2Array.mq4' (28,8)
第 28 行如下:arr[0].date = TimeCurrent();
知道哪里错了吗?
提前致谢 /库尔。
【问题讨论】:
-
好吧,基于索引 0 超出范围的事实,我不得不说
arr的大小为 0。您可能需要初始化它。 -
通过在影响元素到数组之前添加 ArrayResize 来解决问题。必须进行数组初始化。谢谢