Class methods not visible in functions

 

Dear All,

This is my first post on this forum. So far I have managed to find solutions to my problems in other threads but not for this one. And it seems so simple.


I need the class public members to be visible in an entire Expert Advisor, not only in function, within which a given class was instantiated. The goal of this is to have one repository of orders generated by many strategies.

Extract from the class:


class OrderClip
  {
      private:
     
         double clip_price[];
         datetime clip_entrytime[];
         string clip_ordertype[];
         string clip_strategyname[];
         int clip_TP[];
         int clip_SL[];
         int clip_o_expiration[];
                 

      public:

     void Set_newprice (double entryprice) 
         {
         int n=0;
         n = ArraySize(clip_price);
         if(entryprice)
           {
               n++;
               ArrayResize(clip_price,n,0);
               clip_price[n-1]=entryprice;
           }

         }


I want to create a new instance of this class in OnInit function:

        OrderClip OrderQueue1;

this  works fine and I can then refer to the public methods.

        OrderQueue1.Set_newprice(price)


However, when I try to call any of the public methods from OnTick function- doesn't work (undeclared identifier). HOW can I make those methods actually become public so that I can refer to them from any function within the EA?


Thank you in advance fro any meaningful response!


Regards,

Adam

 
The question is: Where do you create the new instance? If you create it on the global scope / outside OnInit() etc., then it will be accessible from everywhere. 
 
Yes, I have thought of this but if I try to declare the new instance before the OnInit() (i.e. where all the #properties and global variables are defined), I get an error "Declaration without type"
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. ypsilon_minus I get an error "Declaration without type"
    Don't out line your code or tell us what you think you did, post the actual code; there are no mind readers here.
  3. You have to define your "class OrderClip { ... };" before you can declare a OrderClip OrderQueue1;
 

You want the actual code? Here you go (attached).  So again the question is: how do I make the class methods visible in ALL functions of the EA? I suppose this question could be answered on its own, even without any code- provided someone actually knows the answer ;)

 
Nowhere above that line do you ever define what an OrderClip is. What part of:
You have to define your "class OrderClip { ... };" before you can declare a OrderClip OrderQueue1;
was unclear?
int OnInit()
  {
   :
   Comment(t);
   
   OrderClip TradeQueue;   // OrderClip has not been defined yet.
 
WHRoeder:
Nowhere above that line do you ever define what an OrderClip is. What part of:
You have to define your "class OrderClip { ... };" before you can declare a OrderClip OrderQueue1;
was unclear?


Which version of the editor are you all using ?

I am using version 5.00 build 1241 from 22nd Dec 2015 and I do not get any of these errors compiling this file (only warnings - see picture below)

I also tested an mqh file and the compiler doesn't care if the class is at the top or the bottom of the file there either, i.e. it can be used before it's declaration in the same file.

I have tested this on several computers and fresh installations on VPS etc so it's not just some quirk of my development box

Very odd.



 

Guys,

Thanks for your inquiries but I knew posting the whole code would be confusing. If you still manage to have some interest, please look at an extract (attached). It's a test function and returns an error because the class is instantiated in OnInit function and one of its methods is called in the testfunction();

@jamescater: I am using v4.00 b950 (22 Dec 2015);


@both: I have no problem instantiating the class, I have a problem with calling methods of an already instantiated class.


Please have a look at the test code, it is much more concise and should clearly demonstrate the issue. The error is at line 51.



Thank you in advance for your HELP!!!

Files:
test2.mq4  4 kb
 

When you create the object using the C++ declaration, its life cycle ends with the block it is defined within.

You may create the object on the global level - then its life cycle covers the entire script.

If you need to access it globally, but actually create the instance first in the OnInit(), you cannot use your construction to create object. You need to declare the pointer at the global level, and assign it an object with the new keyword (e.g. in OnInit), and unload it with the delete keyword (e.g. in OnDeinit).

 
jamescater: Which version of the editor are you all using ?

I am using version 5.00 build 1241 from 22nd Dec 2015 and I do not get any of these errors compiling this file (only warnings - see picture below)

Same version:

I modified your code
   OrderClip TradeQueue;  
   TradeQueue.Get_lastprice(1.0);
and it still compiles; so no problem is visible methods.
has for test2 you have:
int OnInit(){
   OrderClip test;
   :
   test.Set_neworder(price,entrytime,ordertype,strategy); // This compiles just fine
}
 void testfunction ()
 {
   double price;
   test.Get_lastprice(price); // This doesn't compile because there is no test in the function or globally.
 };
class  OrderClip{...};

Now if you move the "OrderClip test;" Outside OnInit() then you get "declaration without type"

If you move the class declaration above the "OrderClip test;" then it compiles just fine.

 

Gentlemen,


I am impressed! Indeed, putting class declaration in front of all functions AND instantiating it right after that seems to do the trick. Two more questions, if you please:

  • is such a global instantiation of a class a good practice or is it a bit like using global variables (faux pas)? if so- is there any more "elegant" way to get one common set of arrays for all functions within EA?
  • how would I go about declaring a pointer? I tried the syntax from the mal4 book:
    test *OrderClip=new test2("test2");

but that gives me more errors than I care to count.


Anyway, thank you for your expert advice, it is very much appreciated!


Best regards,

Adam

Reason: