Compilation errors when using -> for methods on pointers in MQL5

 

I get compilation errors in MQL5 when trying to use methods on pointers via the -> operator (e.g. ptr->SayHello()).

Errors: '>' - operand expected, 'SayHello' - undeclared identifier and others.

MetaEditor build 4873 (February 2025) is used, file .mq5, code is entered manually without typos.


The MQL5 documentation (section 'Object Pointers') states that in MQL5, as in C++, pointers to objects are created via `new`.

In C++, the `->` operator is used to call methods of dynamically created objects. It would seem to work, but it does not compile.

There is no explicit indication in the docs that this method call option is not supported in MQL5. Can anyone explain?


Example code for testing:

class MyClass {
public:
   void SayHello() {
      Print("Привет от указателя!");
   }
};

void OnStart() {
   MyClass* ptr = new MyClass();
   ptr->SayHello(); // Здесь возникают ошибки
   delete ptr;
}

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Sergey Chekhovskikh:

I get compilation errors in MQL5 when trying to use methods on pointers via the -> operator (e.g. ptr->SayHello()).

Errors: '>' - operand expected, 'SayHello' - undeclared identifier and others.

MetaEditor build 4873 (February 2025) is used, file .mq5, code is entered manually without typos.


The MQL5 documentation (section 'Object Pointers') states that in MQL5, as in C++, pointers to objects are created via `new`.

In C++, the `->` operator is used to call methods of dynamically created objects. It would seem to work, but it does not compile.

There is no explicit indication in the docs that this method call option is not supported in MQL5. Can anyone explain?


Example code for testing:


class MyClass {
public:
   void SayHello() {
      Print("Привет от указателя!");
   }
};

void OnStart() {
   MyClass* ptr = new MyClass();
   ptr.SayHello(); // Здесь возникают ошибки
   delete ptr;
}


''MQL5 is based on the concept of the popular programming language C++''

Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

Documentation on MQL5: Language Basics
Documentation on MQL5: Language Basics
  • www.mql5.com
The MetaQuotes Language 5 (MQL5) is an object-oriented high-level programming language intended for writing automated trading strategies, custom...