This is MQL4, but it may give you some direction for your issue with the l-value.
https://www.mql5.com/en/blogs/post/680572
- 2016.09.16
- Stanislav Korotky
- www.mql5.com
This is MQL4, but it may give you some direction for your issue with the l-value.
https://www.mql5.com/en/blogs/post/680572
You can't do assignment like that since you're returning an int, which is the equivalent to 5 = 7;
In order to implement assignment in the way you're attempting you'll need to return a reference to an object whose assignment operator has also been overloaded. ...which might look something like this.
#include <Arrays\List.mqh> class IntWrapper : public CObject { public: int custom_index; int num; void operator = (int n) { num = n; } }; class MyIntList : public CList { public: IntWrapper *operator[](int i) { IntWrapper *iw = this.GetFirstNode(); for(; CheckPointer(iw); iw=iw.Next()) if(iw.custom_index == i) return iw; iw = new IntWrapper(); this.Add(iw); iw.custom_index = i; return iw; } }; void OnStart() { MyIntList list; list[900] = 25; list[5] = 1900; Print(list[900].num); }
You can't do assignment like that since you're returning an int, which is the equivalent to 5 = 7;
In order to implement assignment in the way you're attempting you'll need to return a reference to an object whose assignment operator has also been overloaded. ...which might look something like this.
Hey Nicholi, yes, exactly! I was thinking the same, but I hoped there was a simpler way to do it.
Thanks for the code, I will go through it. Really appreciate the help.
You can't do assignment like that since you're returning an int, which is the equivalent to 5 = 7;
In order to implement assignment in the way you're attempting you'll need to return a reference to an object whose assignment operator has also been overloaded. ...which might look something like this.
Hey Nicholi,
just a quick question. Do I understand correctly that you basically created a linked list, and use that to add elements? So the lookup of a value wouldn't be O(1), but O(n)? If not, just let me know and I'll go back studying your code.
Thanks again.
Robert
Hey Nicholi,
just a quick question. Do I understand correctly that you basically created a linked list, and use that to add elements? So the lookup of a value wouldn't be O(1), but O(n)? If not, just let me know and I'll go back studying your code.
Thanks again.
Robert
Correct, and typically I would use CArrayObj for this use since it is faster than a linked list -- a linked list is just easier to demonstrate. Also, you said you had four elements so O(n) in this case is still faster than running a hashing algo plus lookup. If you have a large data collection and need key: value pairs then obviously you'd need a hash-map (dictionary).
Correct, and typically I would use CArrayObj for this use since it is faster than a linked list -- a linked list is just easier to demonstrate. Also, you said you had four elements so O(n) in this case is still faster than running a hashing algo plus lookup. If you have a large data collection and need key: value pairs then obviously you'd need a hash-map (dictionary).
All of these kind of problems are related to the no implementation for pointers to arrays in the language. this is weird.
C++ was invented before with more capabilities than mql5. why mql5 is c++ with less capabilities.
why not to use Java for metatrader ???????
All of these kind of problems are related to the no implementation for pointers to arrays in the language. this is weird.
C++ was invented before with more capabilities than mql5. why mql5 is c++ with less capabilities.
why not to use Java for metatrader ???????
Java doesn't have pointers either.
mql5 (ex5 actually) is running inside a trading platform, it's managed to protect you. I am sure you don't want your platform to crash each time a coder has made a mistake.
Java doesn't have pointers either.
mql5 (ex5 actually) is running inside a trading platform, it's managed to protect you. I am sure you don't want your platform to crash each
time a coder has made a mistake.
Java have pointers!!!
Java doesn't have pointers exactly like c++ but it makes sure you won't need it in the language.
I mean the following code in java:
double[] array1 = new double[10]; array1[0] = 4; double[] array2 = array1; System.out.println(array2[0]);
In mql5 you have to wrap an array but the problem is that the system dose not recognize your wrappers.
I hadn't
seen your answer till now. Now i'm happy with the language because i have written libraries for mql5 and mql4 to work with and have designed
systematic ways to deal with the underlying platform.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey guys,
I am getting acquainted with the mql language, but I have some knowledge of C++.
I want a data structure where I can link a value to another. I was thinking of a map- (C++) or dictionary- (Python) like structure, but it is an overkill for my purposes. So I was thinking of a custom array type, where the subscript is assigned by the program (e.g.: the index will not be 0, 1, 2, 3, but 2, 10, 3, 900, and the length of the array will still be 4, and I can assigne values to these indices, like: list[900] = 2;). It can be done with operator overloading in C++, but I can't seem to get it work in mql. The official document is not much help.
Here is the class:
and here is a simple use:
But since I can't overload the [] operator by reference, like:
it complains that in
list[index] = 3;
l-value is required.
So my question is: how can I (can I even) overload the subscript operator for the above snipet to work?
Thanks in advance.