How to fix error "objects are passed by reference only"

 

Hello, I have the following piece of code:

struct clickDatapoint {
   datetime time;
   int x;
   int y;
   CLICK_TYPE click_type;
};

datetime minimum(List<clickDatapoint> &data) {
   datetime res = data.get(0).time;
   for(unsigned int i = 1; i < (unsigned int)data.size(); ++i) {
      if(data.get(i).time < res)
         res = data.get(i).time; 
   }
   return res;
}

Now I get the error " 'clickDatapoint' - objects are passed by reference only" at List<clickDatapoint> and I don't really know how to fix it.


 
UnknownInnocent:

Hello, I have the following piece of code:

Now I get the error " 'clickDatapoint' - objects are passed by reference only" at List<clickDatapoint> and I don't really know how to fix it.


What is List? The standard library only contains CList.
 
UnknownInnocent:

Hello, I have the following piece of code:

Now I get the error " 'clickDatapoint' - objects are passed by reference only" at List<clickDatapoint> and I don't really know how to fix it.


Fix it by learning to program:

A struct is not a variable, it a kind of a type for variables and you haven't declared any variable of that type and finally you can only pass a variable to a function and of this type (struct) only by reference.

 
Alexandre Borela #:
What is List? The standard library only contains CList.

Actually it is a custom lib: https://github.com/dingmaotu/mql4-lib

GitHub - dingmaotu/mql4-lib: MQL4/5 Foundation Library For Professional Developers
GitHub - dingmaotu/mql4-lib: MQL4/5 Foundation Library For Professional Developers
  • github.com
MQL4/5 Foundation Library For Professional Developers - GitHub - dingmaotu/mql4-lib: MQL4/5 Foundation Library For Professional Developers
 
Carl Schreiber #:

Fix it by learning to program:

A struct is not a variable, it a kind of a type for variables and you haven't declared any variable of that type and finally you can only pass a variable to a function and of this type (struct) only by reference.

"learning to program", thats why I am asking questions...

I didn't really get it: When I create a list, I define which type its elements should have, for datetime for example List<datetime>. What is the difference to saying List<clickDatapoint> which to me means I am want a list that only contains clickDatapoint-structs. Or do I have to define my datapoint as a class to do this?

 
UnknownInnocent #:

Actually it is a custom lib: https://github.com/dingmaotu/mql4-lib

https://github.com/dingmaotu/mql4-lib/blob/master/Collection/Collection.mqh#L142

One of the problems is this line, the template just substitutes the type and it becomes in your example:

virtual bool      contains(const clickDatapoint value) const

Which is invalid for MQL since clickDatapoint is an object, it should be passed as reference:

"MQL5 uses both methods, with one exception: arrays, structure type variables and class objects are always passed by reference. In order to avoid changes in actual parameters (arguments passed at function call) use the access specifier const. When trying to change the contents of a variable declared with the const specifier, the compiler will generate an error."

virtual bool      contains(const clickDatapoint & value) const

The easiest solution without having to modify the template is to use clickDatapoint pointers:

datetime minimum(List<clickDatapoint*> &data) 
But you would have to remember to delete them.
mql4-lib/Collection.mqh at master · dingmaotu/mql4-lib
mql4-lib/Collection.mqh at master · dingmaotu/mql4-lib
  • github.com
MQL4/5 Foundation Library For Professional Developers - mql4-lib/Collection.mqh at master · dingmaotu/mql4-lib
 

Okay thank you so far. Anyways I decided to switch to a class and also to save the price data as MqlRates:

class ClickDatapoint {
private:
   const MqlRates priceData;
   const CLICK_TYPE clickType;
public:
   ClickDatapoint(const MqlRates &price_data, const CLICK_TYPE &click_type);    
   ~ClickDatapoint();
   const MqlRates getPriceData() const;
   const CLICK_TYPE getClickType() const;
};

ClickDatapoint::ClickDatapoint(const MqlRates &price_data, const CLICK_TYPE &click_type):
   priceData(price_data) ,clickType(click_type) {}

Now in the constructor I get this error:

'MqlRates' - function not defined

Is it only possible to use MqlRates as array or what does this mean?
 
UnknownInnocent #:

Okay thank you so far. Anyways I decided to switch to a class and also to save the price data as MqlRates:

Now in the constructor I get this error:

'MqlRates' - function not defined

Is it only possible to use MqlRates as array or what does this mean?
Looks like MqlRates does not have a copy constructor, I tried:

  MqlRates b;
  MqlRates a(b);
And it shows the same error, I consider it a bug, not sure if it is fixed in the latest metatrader build.
 
Alexandre Borela #:
Looks like MqlRates does not have a copy constructor, I tried:

And it shows the same error, I consider it a bug, not sure if it is fixed in the latest metatrader build.

This means my only way to solve this is to remove const from priceData and init it like this:

ClickDatapoint::ClickDatapoint(const MqlRates &price_data, const CLICK_TYPE &click_type):
   clickType(click_type) 
{
   priceData = price_data;      
}

Or is there a better way to do it?

 
UnknownInnocent #:

This means my only way to solve this is to remove const from priceData and init it like this:

Or is there a better way to do it?

Unfortunately yes, since MqlRates is a builtin type and we can't fix it, this bug has to be reported to MetaQuotes so that they can fix it.

An alternative would be to create a wrapper class that accepts the MqlRates in its constructor and expose the members you want,
that way you could code it to work in that case.
Reason: