Using the include directive

 

Hi

I am trying to make my program modular a bit, so I detached a routine and placed it in a file in the include diretory. As below

//extremas.mqh************************************************************

int alosh()
{
int man[2];
man[0] = 10;
man[1] = 11;
return (man);
}

************************************************************************



in the custom indifcator file I am using the above as below:

****************************************************

...

#include <extremas.mqh>

int init()
{
int alo[2] = alosh();
Comment(alo[0]);

}

****************************************************


this does not print the first item in the array man as expected. whats the tweak?


many thanks
 

Try being a little more explicit on your storage and returns...

return(man[0]);

 

That might do it, but I would also change:

int alo[2];
alo[0] = alosh();

------------------------------------------------------------------------

Ok, I tried your code:

return(man);               seems to return either the address of man or maybe just some garbage.

int alo[2] = alosh();     won't even compile.

 
I am trying to make the routine alosh return a pointer to an array, so that I can refrence its elements "after de_refrence the pointer like c++"in the calling routine.
 

You can hand the function an array to work with, will that satisfy your longings?


//mqh
void alosh(int &man[])
{
man[0] = 10;
man[1] = 11;
}


//mq4
int start()
{
int alo[2];
alosh(alo);
Comment(alo[0]+" "+alo[1]);

}

 
phy:

You can hand the function an array to work with, will that satisfy your longings?


//mqh
void alosh(int &man[])
{
man[0] = 10;
man[1] = 11;
}


//mq4
int start()
{
int alo[2];
alosh(alo);


Comment(alo[0]+" "+alo[1]);

}


alosh(alo);

^^^^^^

this line produces 2 errors:

']' - left square parenthesis expected for array

']' - parameter expected
 

No error here...

//+------------------------------------------------------------------+
//|                                          Phy__TimeToCloseBar.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "https://www.metaquotes.net/"
 
#property indicator_chart_window
 
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
 
 
 
//mqh 
void alosh(int &man[])
{
man[0] = 10;
man[1] = 11;
}
 
 
//mq4
int start()
{
int alo[2];
alosh(alo);
Comment(alo[0]+"  "+alo[1]);
 
}

Compiling 'Phy__Test1234.mq4'...

0 error(s), 0 warning(s)

... or here...

 
//mqh 
#include <alosh.mqh>
 
/*
void alosh(int &man[])
{
man[0] = 10;
man[1] = 11;
}
*/
 
//mq4
int start()
{
int alo[2];
alosh(alo);
Comment(alo[0]+"  "+alo[1]);
 
}
 
Ok,
#include <extremas.mqh>
/*
#import "extremas.mqh"
int alosh();
#import
*/
fixed the errors. but if I use the import section and comment out the include line, the errors reinstate.
apparently the import directive is not the same as the "include" in this case.

Anyway.

many thanks for your help.
 

#include and #import are different animals

#include filename -- Preprocessor replaces this line with the content of the mqh file

#import filename -- Import describes function's return type and parameters called from external DLLs or compiled EX4 libraries
type fn1(type parA, type parB, ...);
...
#import

Reason: