1
votes

Passing each individual args into a constructor when you have 10 or more is tedious and not very maintainable - also makes reading the constructor args quite a drag.

The pattern I want to use is commonly used in Ruby/Node/Python and was hoping there was something similar in MQL4

So I want to pass a key value pair object to the constructor with all the args. Unfortunately I am getting -

struct cannot be defined in param list. 

from the compiler.

I am a total n00b at C and this is my first MetaQuotes4 programme(EA) attempt. I have Googled and trawled the docs. Can't find a thing besides the docs saying that passing classes, arrays and structs can only be done using pass by reference.

The issue is line 10.

Screen shot

it's loaded with compile errors - due to this being a WIP. The rest of the errors I can handle but the struct not allowed in params issue has me baffled. Any help or advice would be awesome.

Maybe there is an alternative pattern?

Thanks in advance.

2
Please don't post images of text. Instead please edit your question to include the faulty code as text as well as the complete and unedited error output as text. - Some programmer dude
Apologies - essential this was a amateurs attempt, and as @user3666197 I seemed to have missed quite a bit in the documentation. - user1230795
I got the same anwser as the one @Bo Pearson suggested on the actuual mql4 forum - but still to no avail. Mql4 documentation is not fun. Thanks again fellow programmers. - user1230795
Welcome to the MQL4-world. The best hit ever made was a "silent" change of the MQL4-language syntax near about the Build-572++. The whole code-base had to be re-programmed due to "new"-MQL4 ( MQL4.5678 and the syntax creep continues ... ) restrictions regarding variable validity scope changed, forbidden literals in names of variables. Nightmare indeed once your code-base has about a few hundreds man*years. And creeping ... - user3666197

2 Answers

2
votes

Be carefull - MQL4 is in many aspects not alike a C++ compiler

While syntax constructors may look similar, real use is restricted or forbidden in .MQL4 compilation by a MetaLang.exe compiler.

So, struct look promising, but will not fit your wish.

As depicted in your print-screen,

...
string symbol = params.symbol;
...

you plan to include also string typed values in you struct.

While this is possible, the MQL4 documentation however implicates restrictions for such struct usage and the only form of struct, that is permitted to be safely used in function call parameter passing is just a SIMPLE STRUCT, i.e. a struct without any string component ( after many years spent in MQL4 domain, you would have been prepared for similar specialities. Some of which include a string which is internally not a string, but a struct and the nightmare has just started ... ( and you can imagine, what that implodes on your DLL-interfacing et al. ).

The structure name can't be used as an identifier (name of a variable or function).

or

It should be noted that in MQL4 structure elements follow one another directly, without alignment.

In C++ such an order is made to the compiler using the following instruction:

#pragma pack(1)

If you want to do another alignment in the structure, use auxiliary members, "fillers" to the right size.

and

With the return operator you can't return any arrays, class objects, variables of compound structure type.

So be carefull and read MQL4 Documentation start-to-end, as some "specialities" are hidden in places, one would hardly expect them a-priori.

0
votes

You can pass it to the constructor like const StructDef &_var_name, for example:

class Order {

  // Define struct.
  struct Params {
    string symbol;
    double market_entry;
    double loss_pips;
    double profit_pips;
  };
  // Struct class variable.
  Params params;

  public:
  Order(const Params &_params) {
    this.params = _params;
  };

}

Then use it like:

Params params = {_Symbol, Ask};
// or...
params.symbol = _Symbol;
params.market_entry = Ask;
// ... then:
Order *order = new Order(params);

See: Structures, Classes and Interfaces.