• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 9:49pm
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 9:49pm
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Money Management Template 8 replies

Trading plan template 53 replies

Compiler problem or code problem ? 3 replies

problem, problem, is it MT4 platform or EA, Please help................ 1 reply

My trading plan template. 6 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 3
Attachments: template <typename T> Problem
Exit Attachments

template <typename T> Problem

  • Last Post
  •  
  • Page 1 2
  • Page 1 2
  •  
  • Post #1
  • Quote
  • First Post: Edited Oct 19, 2021 5:46am Oct 17, 2021 2:22pm | Edited Oct 19, 2021 5:46am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
I have written clearly in a better way in post #11 what I want!
The code:

Inserted Code
      //***********************************************************************************************
      // For test just a simple class and one instance of it: x
      // I want to call several instance dependent on settings so it has to be this way!
      //***********************************************************************************************
      class Cx{
         public:
            void f(){
            }
      };        
      Cx x();
      //***********************************************************************************************
      // My test class with methods
      //***********************************************************************************************
      class CTest{
         public:            
            //*******************************************************************
            // buy
            //******************************************************************      
            template <typename T> void buy(T t){
               // coding here with "t"
            };
              
              
            //***********************************************************************************************
            // verifyAndCloseBuy
            //***********************************************************************************************
            bool verifyAndCloseBuy(int i){              
               // Calling method "buy" with instance object x: works 100%
               this.buy(&x);
               return false;          
            }    
                
  
            //***********************************************************************************************
            // getActTrade
            //***********************************************************************************************
            template <typename T> void getActTrade(T actTrade){  
               // Set my Object to actTrade
               actTrade=&x;
            }
            //***********************************************************************************************
            // verifyAndCloseOpenOrdersEA
            //***********************************************************************************************
              template <typename T> void actionWithSettingActTrade(){  
               T actTrade;
               getActTrade(actTrade);
               this.buy(actTrade);  
            };
                        
      };      
      CTest test();
  
 
      //***********************************************************************************************
      // Call outside the class
      //***********************************************************************************************  
      void testAll(){
         // The rest is compiled......
         test.actionWithSettingActTrade();
      }

results in:
template mismatch test.mqh 62 15
'actionWithSettingActTrade' - cannot to apply template test.mqh 62 15

How to solve?
Attached Image
Attached File
File Type: mqh test.mqh   3 KB | 77 downloads
Vucking good EA coder...
  • Post #2
  • Quote
  • Edited at 9:59pm Oct 17, 2021 9:47pm | Edited at 9:59pm
  •  eess
  • Joined Feb 2021 | Status: Member | 417 Posts | Invisible
Quoting reteid2222
Disliked
The code: //*********************************************************************************************** // For test just a simple class and one instance of it: x // I want to call several instance dependent on settings so it has to be this way! //*********************************************************************************************** class Cx{ public: void f(){ } }; Cx x(); //*********************************************************************************************** // My test class with methods //***********************************************************************************************...
Ignored

I don't really use template functions, but from my testing of your file, it seems to me that template functions need input parameters. If I set an input parameter, even if I don't use that parameter in anyway in the function, it can compile with no issues as follows. Maybe some other experts can advise you better.


template <typename T> void actionWithSettingActTrade(T k){
T actTrade;
getActTrade(actTrade);
this.buy(actTrade);
};

void testAll(){
test.actionWithSettingActTrade(1);
}

Some additional information from this website:
https://www.cplusplus.com/doc/oldtutorial/templates/
 
1
  • Post #3
  • Quote
  • Oct 18, 2021 2:50am Oct 18, 2021 2:50am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
I know this site and it didn t help me anyway.....
Vucking good EA coder...
 
 
  • Post #4
  • Quote
  • Oct 18, 2021 9:08am Oct 18, 2021 9:08am
  •  braintheboss
  • Joined Nov 2012 | Status: Coder | 8,490 Posts
Quoting reteid2222
Disliked
I know this site and it didn t help me anyway.....
Ignored
I didn't test code but i see one thing. If you use T in almost all methods is better use it at class definition. It have some advantages as you can use virtual methods or avoid names conflict.
Try don't lose pants never...
 
1
  • Post #5
  • Quote
  • Oct 18, 2021 10:06am Oct 18, 2021 10:06am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Quoting braintheboss
Disliked
{quote} I didn't test code but i see one thing. If you use T in almost all methods is better use it at class definition. It have some advantages as you can use virtual methods or avoid names conflict.
Ignored
My intention is too use three instances x,y,z of different classes like 3 different trailing stop types and use them in the test class in dependence of the setttings. So the instance of the class CTest which will be used in the EA or my trading simulation won t know the value of T which will be used!
Vucking good EA coder...
 
 
  • Post #6
  • Quote
  • Oct 18, 2021 10:40am Oct 18, 2021 10:40am
  •  braintheboss
  • Joined Nov 2012 | Status: Coder | 8,490 Posts
I think your error is because you missed ";" in 2 previous methods at end of class. Finish methods with }; and check if error disappear. I don't see anything weird at code but if you are using mt4 last metaeditor builds then maybe the problem is that. It have a nasty bug made many weird typo errors even code is fine and compile well in older builds
Try don't lose pants never...
 
 
  • Post #7
  • Quote
  • Oct 18, 2021 2:12pm Oct 18, 2021 2:12pm
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Quoting braintheboss
Disliked
I think your error is because you missed ";" in 2 previous methods at end of class. Finish methods with }; and check if error disappear. I don't see anything weird at code but if you are using mt4 last metaeditor builds then maybe the problem is that. It have a nasty bug made many weird typo errors even code is fine and compile well in older builds
Ignored
Thank you....doesnt work with two more ";" either...
Vucking good EA coder...
 
 
  • Post #8
  • Quote
  • Oct 18, 2021 6:05pm Oct 18, 2021 6:05pm
  •  braintheboss
  • Joined Nov 2012 | Status: Coder | 8,490 Posts
I checked it and I saw problem is you have define type if method havent parameters. Try this:

test.actionWithSettingActTrade<uint>()
Try don't lose pants never...
 
1
  • Post #9
  • Quote
  • Oct 19, 2021 1:14am Oct 19, 2021 1:14am
  •  eess
  • Joined Feb 2021 | Status: Member | 417 Posts | Invisible
I think braintheboss is right, template functions expect function input arguments, if the template function does not define any input arguments, when calling the function, it is still necessary to explicitly supply it when you call the function, braintheboss's solution is calling the function by supplying an "uint" as the function argument.
 
1
  • Post #10
  • Quote
  • Oct 19, 2021 5:39am Oct 19, 2021 5:39am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Quoting braintheboss
Disliked
I checked it and I saw problem is you have define type if method havent parameters. Try this: test.actionWithSettingActTrade<uint>()
Ignored
You can t use Cx or cx for uint.... that s what I needed.....
Vucking good EA coder...
 
 
  • Post #11
  • Quote
  • Oct 19, 2021 5:41am Oct 19, 2021 5:41am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
I try to show it much clearer what I want....
Replace
Inserted Code
               if (typeOfCHOICE==INUM_CHOICES_X)              
                  this.buy(&x);  
               else if (typeOfCHOICE==INUM_CHOICES_Y)
                  this.buy(&y);
with something like:
Inserted Code
               T t;
               setTValue(t);
               this.buy(&t);
Because I use it multiple times in the code.....and different calls like this.otherfunc(&t).....



Inserted Code
      //***********************************************************************************************
      // A list of classes
      //***********************************************************************************************
      enum INUM_CHOICES{
         INUM_CHOICES_X,                                                   // X
         INUM_CHOICES_Y,                                                   // Y
        };
      input INUM_CHOICES typeOfCHOICE=INUM_CHOICES_X;                     // Type of choice    
  
      class Cx{};        
      Cx x();
      
      class Cy{};        
      Cy y();
      //***********************************************************************************************
      // Calling the classes Cx and Cy per user choice
      //***********************************************************************************************
      class CTest{
         public:    
            //*******************************************************************
            // buy
            //******************************************************************      
            template <typename T> void buy(T t){
               // coding here with "t"
            };
                      
            //***********************************************************************************************
            // action
            //***********************************************************************************************
              void action(){  
               // This is my intention:
               if (typeOfCHOICE==INUM_CHOICES_X)              
                  this.buy(&x);  
               else if (typeOfCHOICE==INUM_CHOICES_Y)
                  this.buy(&y);  
            };
                        
      };      
      CTest test();
  
 
      //***********************************************************************************************
      // Call outside the class
      //***********************************************************************************************  
      void testAll(){
         test.action();
      }
Vucking good EA coder...
 
 
  • Post #12
  • Quote
  • Oct 19, 2021 7:12am Oct 19, 2021 7:12am
  •  hal7
  • Joined Apr 2011 | Status: Member | 3,454 Posts
Thanks for unignoring all of us....
 
 
  • Post #13
  • Quote
  • Oct 19, 2021 8:57am Oct 19, 2021 8:57am
  •  eess
  • Joined Feb 2021 | Status: Member | 417 Posts | Invisible
Quoting reteid2222
Disliked
I try to show it much clearer what I want....
Ignored
See whether this works for you:

Inserted Code
#property strict
#property indicator_chart_window
 
  enum INUM_CHOICES{
         INUM_CHOICES_X,                                                   // X
         INUM_CHOICES_Y,                                                   // Y
  };
  
  input INUM_CHOICES typeOfCHOICE=INUM_CHOICES_X;                     // Type of choice  
  
      interface CommonInterface
      {
          void method(){};
      };
 
      class Cx : public CommonInterface {
          void method() override {
              int i=1;
              Print("i: " + (string)i);
              return;
          }
      };
    
      class Cy : public CommonInterface {
          void method() override{
              int i=2;
              Print("i: " + (string)i);
              return;
          }
      };
      
      
       //***********************************************************************************************
      // Calling the classes Cx and Cy per user choice
      //***********************************************************************************************
      class CTest{
         public:    
           CommonInterface *CIntA;
           CommonInterface *CIntB;
          
           virtual void Create(){
                if (typeOfCHOICE==INUM_CHOICES_X){      
                  CIntA = new Cx;
                  buy(CIntA);
               }
               else if (typeOfCHOICE==INUM_CHOICES_Y){
                  CIntB = new Cy;
                  buy(CIntB);
               }
           };
            
            
           void buy(CommonInterface *myInterface) {
                  myInterface.method();
           };
                      
            ~CTest(){
                 delete CIntA;
                 delete CIntB;
            };  
                        
    };      
      
CTest test;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   test.Create();
//--- indicator buffers mapping
  
//---
   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
  
}  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
2
  • Post #14
  • Quote
  • Oct 19, 2021 10:01am Oct 19, 2021 10:01am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Quoting eess
Disliked
{quote} See whether this works for you: #property strict #property indicator_chart_window enum INUM_CHOICES{ INUM_CHOICES_X, // X INUM_CHOICES_Y, // Y }; input INUM_CHOICES typeOfCHOICE=INUM_CHOICES_X; // Type of choice interface CommonInterface { void method(){}; }; class Cx : public CommonInterface { void method() override { int i=1; Print("i: " + (string)i); return; } }; class Cy : public CommonInterface { void method() override{ int i=2; Print("i: " + (string)i); return; } }; //***********************************************************************************************...
Ignored
100% what I need.....thank you sooooooooooooooo much!
Attached Image
Vucking good EA coder...
 
1
  • Post #15
  • Quote
  • Oct 19, 2021 10:42am Oct 19, 2021 10:42am
  •  braintheboss
  • Joined Nov 2012 | Status: Coder | 8,490 Posts
Quoting reteid2222
Disliked
{quote} You can t use Cx or cx for uint.... that s what I needed.....
Ignored
Just replace uint for type you want. Example was for you see it need set type for methods without parameters. Templates just are compiler definition replacement
Try don't lose pants never...
 
 
  • Post #16
  • Quote
  • Oct 19, 2021 11:34am Oct 19, 2021 11:34am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Quoting hal7
Disliked
Thanks for unignoring all of us....
Ignored
Thanks for filling my threads with comments I don t need....
Vucking good EA coder...
 
1
  • Post #17
  • Quote
  • Oct 19, 2021 2:25pm Oct 19, 2021 2:25pm
  •  hal7
  • Joined Apr 2011 | Status: Member | 3,454 Posts
Quoting reteid2222
Disliked
{quote} Thanks for filling my threads with comments I don t need....
Ignored
 
 
  • Post #18
  • Quote
  • Oct 21, 2021 5:57am Oct 21, 2021 5:57am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Thank you for all you help..... the question was how to use 2 or more classes in a class dependent on settings....all code in in one file and can be include and the main class does not know anything about this part.
The idea was to create an interface so that all helper classes could be declared as parameter and used as arguments!
My final solution looks like this:
Inserted Code
   #property strict
   #property indicator_chart_window
    
     enum INUM_CHOICES{
            INUM_CHOICES_X,                                                   // X
            INUM_CHOICES_Y,                                                   // Y
     };
    
     input INUM_CHOICES typeOfCHOICE=INUM_CHOICES_X;                     // Type of choice  
    
         interface Cxyz
         {
             void method(){};
         };
    
         class Cx : public Cxyz {
             void method() override {
                  Alert("1 "+(string)TimeCurrent());                
             }
         };
      
         class Cy : public Cxyz {
             void method() override{
                  Alert("2 "+(string)TimeCurrent());                
             }
         };
        
        
          //***********************************************************************************************
         // Calling the classes Cx and Cy per user choice
         //***********************************************************************************************
         class CTest{
            public:    
              Cxyz *getInstance() {
                  if (typeOfCHOICE==INUM_CHOICES_X)    
                     return new Cx;
                  else if (typeOfCHOICE==INUM_CHOICES_Y)
                     return new Cy;
                  // For compiler - will never happen!
                  else
                     return NULL;
              };
                              
              
              void action() {
                  // Similar calls will be used several times in different methods in the class!
                  buy(getInstance());
              };
              
              void buy(Cxyz *cxyz) {
                     cxyz.method();
              };                                                    
       };      
        
   CTest test;
  
   int OnInit(){
      // Main part does not know which cx or cy to use
      test.action();
      return(INIT_SUCCEEDED);
     }
    
   int start(){return -1;     }
Vucking good EA coder...
 
 
  • Post #19
  • Quote
  • Oct 21, 2021 6:47am Oct 21, 2021 6:47am
  •  eess
  • Joined Feb 2021 | Status: Member | 417 Posts | Invisible
Quoting reteid2222
Disliked
Thank you for all you help..... the question was how to use 2 or more classes in a class dependent on settings....all code in in one file and can be include and the main class does not know anything about this part. The idea was to create an interface so that all helper classes could be declared as parameter and used as arguments! My final solution looks like this
Ignored
Looks good, just one thing, when you're allocating memory dynamically with "new", need to explicitly call "delete" somewhere when you do not need it anymore to prevent memory leakage. Refer to the code I sent earlier..in the class destructor.
 
 
  • Post #20
  • Quote
  • Oct 21, 2021 7:00am Oct 21, 2021 7:00am
  •  reteid2222
  • Joined Aug 2015 | Status: Member | 2,507 Posts
Quoting eess
Disliked
{quote} Looks good, just one thing, when you're allocating memory dynamically with "new", need to explicitly call "delete" somewhere when you do not need it anymore to prevent memory leakage. Refer to the code I sent earlier..in the class destructor.
Ignored
My idea was when a new function call is created then all code and space for variables and instances are created new and destructed after the function call... so here should be no delete of the instance necessary from my point of view and understanding...
Thanx again...
Vucking good EA coder...
 
 
  • Platform Tech
  • /
  • template <typename T> Problem
  • Reply to Thread
    • Page 1 2
    • Page 1 2
0 traders viewing now
  • More
Top of Page
Forex Factory Blog Updated: Alerting All Members
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2022