2

I tried to create class Complex number in C# with two different constructor, the first constructor takes real part and imaginary part, the second constructor takes module and argument.

public class Complex
{
    public Complex() { }

    private Complex(double _re, double _im)
    {
        re = _re;
        im = _im;
    }

    public static double Complex_FromCartesian(double _re, double _im)
    {
        return new Complex(_re, _im);
    }

    public static double Complex_FromPolar(double _mod, double _arg)
    {
        var _re = _mod * Math.Cos(_arg);
        var _im = _mod * Math.Sin(_arg);
        return new Complex(_re, _im);
    }

    public static Complex operator +(Complex num1, Complex num2)
    {
        return new Complex(num1.re + num2.re, num2.im + num2.im);
    }

    public static Complex operator -(Complex num1, Complex num2)
    {
        return new Complex(num1.re - num2.re, num2.im - num2.im);
    }

    public double Re { get; set; }
    public double Im { get; set; }

    private double re, im;
}

}

but I got the same error in both constructors
enter image description here
How to fix that?

5
  • 2
    Change the return type of these static functions to Complex? Commented Jul 4, 2013 at 8:52
  • 1
    the method Complex_FromCarhesion returns double in the header but you want to return a Complex (return new Complex). Change in the header to return a Complex. Commented Jul 4, 2013 at 8:53
  • 1
    Either return Complex or override implicit cast in Complex. Commented Jul 4, 2013 at 8:54
  • A side note: Why are you using static methods instead of real contructors? Commented Jul 4, 2013 at 8:57
  • @Treb Because those static methods would have the same signature if they were converted to constructors. TimeSpan struct is good example of this. I really don't understand why Complex is not struct here. Commented Jul 4, 2013 at 9:03

5 Answers 5

8

Your method returns a double but you're trying to return a Complex type

Change:

public static double Complex_FromCartesian(double _re, double _im)
{
    return new Complex(_re, _im);
}

To:

public static Complex Complex_FromCartesian(double _re, double _im)
{
    return new Complex(_re, _im);
}
Sign up to request clarification or add additional context in comments.

2 Comments

But how to use this constructor?
It's not a constructor. This is a static method that instanciates your class type. Call is like Complex c = Complex.Complex_FromCartiesian(x, y);...
6

Change the return type of that method to Complex.

Comments

5

You cant return Complex when double is expected.

public static Complex Complex_FromCartesian(double _re, double _im)
{
   return new Complex(_re, _im);
}

Comments

3

Your constructor is fine. The problem is that you've specified a return type of double and you're returning a class of type "Complex".

Comments

2

This error occurs when the data types mismatch. It is expecting "complex" datatype and you are telling it is double. Change double to complex

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.