Could be one of 2 functions.....

 

I am trying to use CArrayList with a custom class, but when I try to Add to it, it doesn't seem to know which function to use - is this a bug in the platform? How can I specify?



class EntryLevel {
public:
void EntryLevel(double p, bool d) : price(p), direction(d) {}
        double price;
        bool direction;
};


#include <Generic\ArrayList.mqh>
CArrayList<EntryLevel*> entryLevels;


entryLevels.Add(EntryLevel(15.0, true));

 

You are passing a class name (which is just a kind of template) while you should pass an object to the Add method.

   EntryLevel *lvl=new EntryLevel(15.0,true);
   entryLevels.Add(lvl);
 
  1. Amir Yacoby #: You are passing a class name (which is just a kind of template) while you should pass an object to the Add method.
    It's not a “Kind of template.” That is calling the EntryLevel constructor.
    void EntryLevel(double p, bool d)

  2. WorthyVII: I am trying to use CArrayList with a custom class, but when I try to Add to it, it doesn't seem to know which function to use - is this a bug in the platform? How can I specify?

    How To Ask Questions The Smart Way. (2004)
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  3. entryLevels is a list of pointers.
    You are not passing a pointer, you are passing the object.
    CArrayList<EntryLevel*> entryLevels;
    entryLevels.Add(    EntryLevel(15.0, true));
    Pass the pointer.
    entryLevels.Add(new EntryLevel(15.0, true));

 
William Roeder #:
  1. It's not a “Kind of template.” That is calling the EntryLevel constructor.

No, not correct. 

The code of OP is not calling the construcor - it just passes the class name.

EntryLevel(15.0,true)        ==> This is the ops code -  is not calling constructor
new EntryLevel(15.0,true)    ==> This calls the constructor

And a class is a kind of template.

Hope I'm not in the block list now.

Shana Tova

 
William Roeder #:
  1. entryLevels is a list of pointers.
    You are not passing a pointer, you are passing the object.
    Pass the pointer.

He is not "passing the object" - he is passing a class name with parameters - which is wrong. What I gave is passing the object explicitly, what you gave at 3 is the same, unexplicitely (and only if the OP doesn't need the pointer).

Of course I preffered not to show that as It is clear that OP does not know those nuances so best to write as I wrote and not show how smart I am..  

 
Amir Yacoby #: No, not correct.  The code of OP is not calling the construcor - it just passes the class name.
EntryLevel(15.0,true)        ==> This is the ops code -  is not calling constructor
new EntryLevel(15.0,true)    ==> This calls the constructor

Wrong! You can not create an object without calling its constructor. Change the constructor (e.g. add a third argument) and both lines no longer compile.

 
William Roeder #:

Wrong! You can not create an object without calling its constructor. Change the constructor (e.g. add a third argument) and both lines no longer compile.

Of course, what's new here?  oops.. double meaning.

The parameter to Add is an object which is created with 'new' - same in my example and yours. 

And to clarify for the OP

The constructor is like a static method (class method), you can call it even when there's not an object yet (actually, that's what it's for - creating an object).
But, OO languages usually use the 'new' for calling a constructor as a way to make sure you understand that this call returns an object of the class template(template - just using it as a means to explain to NOOB what is a class, because there's something else called 'class template' not meant here). Not using the 'new' is just wrong syntax as the outcome of constructor by definition must be a new object.

 
Thanks guys, I don't know why I thought the "new" was unneccessary for some reason. I thought I read somewhere that it was not needed and just forgot about it. Thanks!
Reason: