骆闻舟家的费嘟嘟 2022-04-24 15:59 采纳率: 80%
浏览 20
已结题

给复数赋值时出现Object reference not set to an instance of an object问题

img


请问大噶,这段代码想给复数的实部与虚部赋值,但是一直出现这种错误是为什么?
下面是关于复数类的定义
using System;
using System.Collections.Generic;
using System.Text;

namespace ems004
{
//复数类的一个定义
public class Complex
{
public Complex()
: this(0, 0)
{
}

    /// <summary>
    /// 只有实部的构造函数
    /// </summary>
    /// <param name="real">实部</param>
    public Complex(double real)
        : this(real, 0) { }

    /// <summary>
    /// 由实部和虚部构造
    /// </summary>
    /// <param name="real">实部</param>
    /// <param name="image">虚部</param>
    public Complex(double real, double image)
    {
        this.real = real;
        this.image = image;
    }

    private double real;
    /// <summary>
    /// 复数的实部
    /// </summary>
    public double Real
    {
        get { return real; }
        set { real = value; }
    }

    private double image;
    /// <summary>
    /// 复数的虚部
    /// </summary>
    public double Image
    {
        get { return image; }
        set { image = value; }
    }

    ///重载加法
    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.real + c2.real, c1.image + c2.image);
    }

    ///重载减法
    public static Complex operator -(Complex c1, Complex c2)
    {
        return new Complex(c1.real - c2.real, c1.image - c2.image);
    }

    ///重载乘法
    public static Complex operator *(Complex c1, Complex c2)
    {
        return new Complex(c1.real * c2.real - c1.image * c2.image, c1.image * c2.real + c1.real * c2.image);
    }


    /// <summary>
    /// 求复数的模
    /// </summary>
    /// <returns></returns>
    public double ToModul()
    {
        return Math.Sqrt(real * real + image * image);
    }
}

}

  • 写回答

1条回答 默认 最新

  • Null_Reference 2022-04-24 16:08
    关注

    input7是一个对象数组 你个某个对象属性赋值 得先有这个对象才有这个属性。
    var complex=new Complex();
    complex.Real=real[i];
    complex.Image=imag[i];
    input[7]= complex;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月2日
  • 已采纳回答 4月24日
  • 创建了问题 4月24日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度