Download MetaTrader 5

(Bug) Functions that returns struct

To add comments, please log in or register
Jin
1539
Jin  

Rosh,

There's some problem with functions that returns struct.

The compilation goes with no error, but the code simply doesn't run, no error alert of any kind, the code simply does nothing.

 I created a code demo to show you. (I would put this in the Service Desk instead of here, but I havent' enough rate to post there this bug).

 Please help.

//----------------------------------------------------- 

struct Info
{
   double propA;
   double propB;
   double propC;
};

void OnStart()
{
   Info info;

   info = Dummy();
   Print(info.propA);
   Print(info.propB);
   Print(info.propC);
}


Info Dummy()
{
   Info result;
  
   result.propA = 1.1;
   result.propB = 2.2;
   result.propC = 3.3;

   return result;
}

//----------------------------------------------------- 

Paul
784
Paul  
Jin posted  :

Rosh,

There's some problem with functions that returns struct.

The compilation goes with no error, but the code simply doesn't run, no error alert of any kind, the code simply does nothing.

 I created a code demo to show you. (I would put this in the Service Desk instead of here, but I havent' enough rate to post there this bug).

 Please help.

//----------------------------------------------------- 

struct Info
{
   double propA;
   double propB;
   double propC;
};

void OnStart()
{
   Info info;

   info = Dummy();
   Print(info.propA);
   Print(info.propB);
   Print(info.propC);
}


Info Dummy()
{
   Info result;
  
   result.propA = 1.1;
   result.propB = 2.2;
   result.propC = 3.3;

   return result;
}

//----------------------------------------------------- 

 

This isn't a bug - it would happen in any C++ code.  The problem is that Info result only exists during the life of the Dummy function call.

You could code it like this

 

struct Info
{
   double propA;
   double propB;
   double propC;
};

void OnStart()
{
   Info info; 

   Dummy(info);
   Print(info.propA);
   Print(info.propB);
   Print(info.propC);
}


void Dummy(Info& inoutInfo)
{
   inoutInfo.propA = 1.1;
   inoutInfo.propB = 2.2;
   inoutInfo.propC = 3.3;
}

 

Paul

 

 

Rashid Umarov
Admin
12315
Rashid Umarov  
Jin posted  :

Rosh,

There's some problem with functions that returns struct.

The compilation goes with no error, but the code simply doesn't run, no error alert of any kind, the code simply does nothing.


It is not allowed to return neither a structure or a class from function. You can pass your structure into function as parameter and set it's needed properties or you can return class object pointer (structures don't have a pointers).

See sample which Paul provided.

Rashid Umarov
Admin
12315
Rashid Umarov  
Jin posted  :

The compilation goes with no error, but the code simply doesn't run, no error alert of any kind, the code simply does nothing.

How  have you run your sample - on usual way into terminal or in debug way from editor?

Rashid Umarov
Admin
12315
Rashid Umarov  
Rosh:

It is not allowed to return neither a structure or a class from function. You can pass your structure into function as parameter and set it's needed properties or you can return class object pointer (structures don't have a pointers).

My answer was not quite right. You can return from function simple structure. Under simple structure I mean a structure which does not contain any dynamic array, or a string, or a class object, or a pointer, or another nonsimple structure.
Jin
1539
Jin  
Rosh:

How  have you run your sample - on usual way into terminal or in debug way from editor?

Rosh,

I had tested the code in debug way only.

On usual way, I have tested now and there's no problem, the code runs corretly. Thanks.

Any way, could you please ask the development team to fix it in the debug mode.

 

Thanks !!! 

Jin
1539
Jin  
phampton:

This isn't a bug - it would happen in any C++ code.  The problem is that Info result only exists during the life of the Dummy function call.

You could code it like this

 

 

Paul

 

 

Paul,

It's an good alternative way to make this code work, thanks.

But in fact, the code should work (and works on not debug mode).

Ilyas
1202
Ilyas  
Jin:

 

But in fact, the code should work (and works on not debug mode). 

 

You are right, it is a bug. I will fix it.

Ilyas
1202
Ilyas  

Bug fixed, please wait for updates.

Paul
784
Paul  
Jin:

Paul,

It's an good alternative way to make this code work, thanks.

But in fact, the code should work (and works on not debug mode).

 

My apologies - learn something every day, as they say.  This syntax is a carryover from C and in checking further I see that it is valid C/C++.

Paul 

Andre Tavares
723
Andre Tavares  
Paul:

This isn't a bug - it would happen in any C++ code.  The problem is that Info result only exists during the life of the Dummy function call.

You could code it like this

 

 

Paul

 

 

This code works..
To add comments, please log in or register