Download MetaTrader 5
To add comments, please log in or register
Need mobile version of MetaTrader 5? Download it and trade!
Jin
1538
Jin 2010.03.11 04:44 

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 2010.03.11 10:27  
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
11702
Rashid Umarov 2010.03.11 10:56  
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
11702
Rashid Umarov 2010.03.11 14:06  
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
11702
Rashid Umarov 2010.03.11 14:16  
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
1538
Jin 2010.03.11 15:34  
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
1538
Jin 2010.03.11 15:43  
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
1205
Ilyas 2010.03.11 15:47  
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
1205
Ilyas 2010.03.11 17:12  

Bug fixed, please wait for updates.

Paul
784
Paul 2010.03.12 00:47  
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 

/
To add comments, please log in or register