simple question about calling class/struct

 

Hi experts,


I am currently trying to get my first function to be compiled. But I got some error. 

any idea how to fix them?

I created 2 mqh files and want the second file calling the struct of first to make a function.

//test.mqh
#include <test2.mqh>


struct ex
  {
   int a;
   int b;
   ex(){a=0;b=0;}
  };





//test2.mqh
#include <test.mqh>


ex ok[];
ArrayResize(ok,5);


void callingstruct(ex &array[]){

array[1].a=50;
array[1].b=50;

}





Thank in advance,Kittamet



Thank in advance

 
Kittamet Sirinyamas:

Hi experts,

I am currently trying to get my first function to be compiled. But I got some error. 

any idea how to fix them?

I created 2 mqh files and want the second file calling the struct of first to make a function.

Thank in advance,Kittamet

Thank in advance

This line:

ArrayResize(ok,5);

Cannot be in global scope. Place it within the function that requires it.

 
Seng Joo Thio:

This line:

Cannot be in global scope. Place it within the function that requires it.

still got error in test.mqh


thank you

 
Kittamet Sirinyamas:

still got error in test.mqh

thank you

Then add this line into test2.mqh (after line #include):

struct ex;
That's "forward declaring" the structure that will be defined later.
 
Seng Joo Thio:

Then add this line into test2.mqh (after line #include):

That's "forward declaring" the structure that will be defined later.

Thank you very much

compiler now have no error.

 
Kittamet Sirinyamas:

Hi experts,


I am currently trying to get my first function to be compiled. But I got some error. 

any idea how to fix them?

I created 2 mqh files and want the second file calling the struct of first to make a function.

Not sure about your logic or context, but does it work ok if you just run the whole code from the one file, thats your first step...?

If you're just trying to alter the values of the struct members there are probably easier ways...

struct ex{  
   int a,b;
   void ex1(int i,int j){a+=i;b+=j;}
  };
 ex ok[];

void OnInit(){
 ArrayResize(ok,5);
 ok[1].ex1(10,20);
}

or even

struct ex{  
   int a,b;
  };
 ex ok[];

void OnInit(){
 ArrayResize(ok,5);
 ok[1].a+=10;ok[1].b+=100;
}
 

Personally I don't use include files anymore, because the auto-complete and function find features of the editor are of no use when you're working between files.

 
andrew:

Not sure about your logic or context, but does it work ok if you just run the whole code from the one file, thats your first step...?

If you're just trying to alter the values of the struct members there are probably easier ways...

or even

Personally I don't use include files anymore, because the auto-complete and function find features of the editor are of no use when you're working between files.

Hi,andrew


you are right.if these code are assembled to a script. this problem will not happen.

I am trying to create any include and then linking in to one for easy understading and editing.

The code will be seperated to part by part that work uniquely.I have seen too many delevoper do like this , but I dont know what it is. I just learning

Reason: