Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 93
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Prints can only be inserted inside a function, but I don't understand how to call this function to make the print or alert work. Can you help by fixing my code with three simple variables? I do everything as described in the article, but the editor does not show up, or something is missing, or something is missing, in any case, the wall, so I asked for help. Thanks for the SKC.
You are outputting d with an alert.
Where is it calculated?
//| TestASD.mq4 |
//| Copyright 2017, Artem A. Trishkin, Skype artmedia70 |
//| https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Artem A. Trishkin, Skype artmedia70"
#property link "https://login.mql5.com/ru/users/artmedia70"
#property version "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input int InputA=1;
input int InputB=3;
class ASD
{
private:
// Здесь располагаются переменные и функции, доступные только внутри класса
int c(int m_a,int m_b)
{
m_a=a+b; m_b=a-b;
int m_d=m_a+m_b;
d=m_d;
Print(__FUNCTION__);
return(c(m_a,m_b));
}
public:
int a;
int b;
int d;
};
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
ASD ar;
void OnStart()
{
//---
ar.a=InputA;
ar.b=InputB;
Alert("ar.a=",ar.a,"\nar.b=",ar.b,"\nar.d=",ar.d);
}
//+------------------------------------------------------------------+
Inside the class, make Prints to values. And then look in the log what will be printed when running the script ;)
And so it's also okay.
class:
{
private:
// Здесь располагаются переменные и функции, доступные только внутри класса
int a_s;
int a_d;
int d;
int g ()
{d=a_s+a_d;
return (g());
}
public:
void fg (int a,int b)
{ a_s=a;
a_d=b;
}
void sf ()
{g ();
Alert (d);}
};
the script
ASD ar;
void OnStart()
{
ar.fg (3,5);
ar.sf ();
}
And so it's also okay.
class:
And so also nothing
//| TestASD.mq4 |
//| Copyright 2017, Artem A. Trishkin, Skype artmedia70 |
//| https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, Artem A. Trishkin, Skype artmedia70"
#property link "https://login.mql5.com/ru/users/artmedia70"
#property version "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input int InputA=1;
input int InputB=3;
class ASD
{
private:
// Здесь располагаются переменные и функции, доступные только внутри класса
public:
int a;
int b;
int d;
int c(int m_a,int m_b)
{
m_a=a+b; m_b=a-b;
d=m_a+m_b;
Print(__FUNCTION__);
return(d);
}
};
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
ASD ar;
void OnStart()
{
//---
ar.a=InputA;
ar.b=InputB;
Alert("ar.a=",ar.a,"\nar.b=",ar.b,"\nar.d=",ar.d,"\nar.c(",ar.a,",",ar.b,")=",ar.c(ar.a,ar.b));
}
//+------------------------------------------------------------------+
It's not clear - what do you want to get?
I do:
1. to pass data from the script to the class
2. sum these data in the class
3. pass the sum from the class to the script
4. from the script, print the sum of the data to a monitor or printer.
Everything!
I do:
1. pass data from the script to the class
2. sum these data in the class
3. pass the sum from the class to the script
4. From the script, output the sum of the data to a monitor or printer.
All!
See above example
But it also does not calculate the sum of variables a+b=d, while I need the sum of d!
I didn't show you exactly what it should count. I showed you how to get the calculated value - call the c() function;
You didn't call it anywhere, and waited for d to somehow calculate itself.
And what the c() function calculates is up to you
I didn't show you exactly what it should count. I showed you how to get the calculated value - call the c() function;
You have not called it anywhere, and you were waiting for d to somehow calculate itself.
And you are to decide for yourself what the c() function counts!
I know and use how to create and call the function in the Expert Advisor. The question is about classes!
I didn't show you exactly what it should count. I showed you how to get the calculated value - call the c() function;
You didn't call it anywhere, and you were waiting for d to somehow calculate itself.
And what the c() function calculates is up to you
That's the point, how to call a function inside a class, namely, how private: public:, how to pass data to a class and how to get the result from the class. So far this is it!
But your function does not calculate anything either.