How return a class instance from a function?

 

I have a custom class in MQL4 and I would like to create a function that returns a instance of it.

Here is my code:


FracRes* calcFractal(double x, double y, bool x_, bool y_)
{
	FracRes *res = new FracRes(x_, x, y_, y);
	
	return res;
}


class FracRes
{
    private:
        bool isTop;
        double Top;
        bool isBottom;
        double Bottom;
    
    public:
        // Default constructor
        FracRes(void);
        // Parametric constructor
        FracRes(bool is_top, double top, bool is_bottom, double bottom)
        {
            isTop = is_top;
            Top = top;
            isBottom = is_bottom;
            Bottom = bottom;
        };
};

What am I doing wrong?

 
mdragganov:

I have a custom class in MQL4 and I would like to create a function that returns a instance of it.

Here is my code:


What am I doing wrong?

Declare the class first and then use it in functions. You can forward declare at the top of your code.
 
Laszlo Tormasi:
Declare the class first and then use it in functions. You can forward declare at the top of your code.

Thanks a lot, Laszlo !

Since I am coming from C# background that is really strange for me {my classes in C# are always at the very end of the file}. Anyway I put the class declaration first and it worked OK!

:)

Laszlo Tormasi
Laszlo Tormasi
  • 2020.09.02
  • www.mql5.com
Trader's profile