C++,退出代码-1073741819

第一行输入三个数字后enter,然后直接“进程已结束,退出代码-1073741819 (0xC0000005)
”,clang-tidy显示没有问题,这是哪里出问题了


#include <iostream>
#include <string>
using namespace std;
int main()
{
    int nn,mm,q;
    cin >>nn >> mm >> q;
    struct Point
    {
        bool is_this_dark = false;
        bool is_this_crossing = false;
        int position = 0;
        Point * latter = nullptr;
        Point * former = nullptr;
    };
    struct Line
    {
        int position = 0;
        string on = "mango";
        Point * point = nullptr;
        Point * first_point = nullptr;
        Line * latter = nullptr;
        Line * former = nullptr;
    };
    class Light
    {
    public:
        Light(int n1,int m1)
        {
            this->n = n1;
            this->m = m1;
            this->hor_lines = &the_first_line_for_hor;
            this->ver_lines = &the_first_line_for_ver;
            this->first_hor_lines = this->hor_lines;
            this->first_ver_lines = this->ver_lines;
            this->hor_lines->point->former = nullptr;
            this->ver_lines->point->former = nullptr;
            this->hor_lines->point->latter = nullptr;
            this->ver_lines->point->latter = nullptr;
            this->hor_lines->first_point = this->hor_lines->point;
            this->ver_lines->first_point = this->ver_lines->point;
            this->hor_lines->latter = nullptr;
            this->ver_lines->latter = nullptr;
            this->hor_lines->former = nullptr;
            this->ver_lines->former = nullptr;
            this->temp_hor_lines = this->hor_lines;
            this->temp_ver_lines = this->ver_lines;
        }
        void Create_new_line()
        {
            cin >> direction;
            if (direction == standard)
            {

                cin >> hor_lines->position >> hor_lines->on;
                handle_hor_line(hor_lines->on == "on");
            }
            else
            {
                cin >> ver_lines->position >> ver_lines->on;
                handle_ver_line(ver_lines->on == "on");
                this->ver_lines->latter->former = this->ver_lines;
                this->ver_lines = this->ver_lines->latter;
            }
        }
        void handle_hor_line(bool is_this_on)
        {
            if (is_this_on)
            {
                while (this->temp_ver_lines->latter != nullptr)
                {
                    while(this->temp_ver_lines->point->latter != nullptr)
                    {
                        if (this->temp_ver_lines->point->position == hor_lines->position)
                        {
                            this->temp_ver_lines->point->is_this_dark = false;
                            this->temp_ver_lines->point->is_this_crossing = true;
                        }
                        this->temp_ver_lines->point = this->temp_ver_lines->point->latter;
                    }
                    this->temp_ver_lines->point = this->temp_ver_lines->first_point;
                }
                this->temp_ver_lines = first_ver_lines;
            }
            else
            {
                while (this->temp_hor_lines->latter != nullptr)        //循环检测是否与白线重合,若有则消除
                {
                    if (this->temp_hor_lines->position == this->hor_lines->position)
                    {
                        this->temp_hor_lines->former->latter = this->temp_hor_lines->latter;        //删除重合的白线
                        this->temp_hor_lines->latter->former = this->temp_hor_lines->former;
                    }
                    this->temp_hor_lines = this->temp_hor_lines->latter;
                }
                temp_hor_lines = first_hor_lines;
                while (this->temp_ver_lines->latter != nullptr)     //将经过的点调整为暗点并设置为未相交
                {
                    while(this->temp_ver_lines->point->latter != nullptr)
                    {
                        if(this->temp_ver_lines->point->position == this->hor_lines->position)
                        {
                            this->temp_ver_lines->point->is_this_crossing = false;
                            this->temp_ver_lines->point->is_this_dark = true;
                        }
                        this->temp_ver_lines->point = this->temp_ver_lines->point->latter;
                    }
                    this->temp_ver_lines->point = this->temp_ver_lines->first_point;
                    this->temp_ver_lines = this->temp_ver_lines->latter;
                }
                this->temp_ver_lines = this->first_ver_lines;
            }
        }
        void handle_ver_line(bool is_this_on)
        {
            if (is_this_on)
            {
                while (this->temp_hor_lines->latter != nullptr)
                {
                    while(this->temp_hor_lines->point->latter != nullptr)
                    {
                        if (this->temp_hor_lines->point->position == hor_lines->position)
                            this->temp_hor_lines->point->is_this_dark = false;
                        this->temp_hor_lines->point = this->temp_hor_lines->point->latter;
                    }
                    this->temp_hor_lines->point = this->temp_hor_lines->first_point;
                }
                this->temp_ver_lines = first_ver_lines;
            }
            else
            {
                while (this->temp_ver_lines->latter != nullptr)        //循环检测是否与白线重合,若有则消除
                {
                    if (this->temp_ver_lines->position == this->ver_lines->position)
                    {
                        this->temp_ver_lines->former->latter = this->temp_ver_lines->latter;        //删除重合的白线
                        this->temp_ver_lines->latter->former = this->temp_ver_lines->former;
                    }
                    this->temp_ver_lines = this->temp_ver_lines->latter;
                }
                this->temp_ver_lines = this->first_ver_lines;
                while (this->temp_hor_lines->latter != nullptr)
                {
                    while(this->temp_hor_lines->point->latter != nullptr)
                    {
                        if(this->temp_hor_lines->point->position == this->ver_lines->position)
                            this->temp_hor_lines->point->is_this_dark = true;
                        this->temp_hor_lines->point = this->temp_hor_lines->point->latter;
                    }
                    this->temp_hor_lines->point = this->temp_hor_lines->first_point;
                    this->temp_hor_lines = this->temp_hor_lines->latter;
                }
                this->temp_hor_lines = this->first_hor_lines;
            }
        }

        int calculate_and_report_lights()
        {
            while (this->temp_hor_lines->latter != nullptr)     //计算横线
            {
                this->on_lights_numbers += n;
                while (this->temp_hor_lines->point->latter != nullptr)
                {
                    if (this->temp_hor_lines->point->is_this_dark)
                        on_lights_numbers--;
                    this->temp_hor_lines->point = this->temp_hor_lines->point->latter;
                }
                this->temp_hor_lines->point = this->temp_hor_lines->first_point;
                this->temp_hor_lines = this->temp_hor_lines->latter;
            }
            this->temp_hor_lines = this->first_hor_lines;
            while (this->temp_ver_lines->latter != nullptr)     //计算横线
            {
                this->on_lights_numbers += m;
                while (this->temp_ver_lines->point->latter != nullptr)
                {
                    if (this->temp_ver_lines->point->is_this_dark && !this->temp_ver_lines->point->is_this_crossing)
                        on_lights_numbers--;
                    this->temp_ver_lines->point = this->temp_ver_lines->point->latter;
                }
                this->temp_ver_lines->point = this->temp_ver_lines->first_point;
                this->temp_ver_lines = this->temp_ver_lines->latter;
            }
            this->temp_ver_lines = this->first_ver_lines;
            return this->on_lights_numbers;
        }
        int time = 0;
    private:
        Line * hor_lines;       //横线
        Line * ver_lines;       //竖线
        Line * first_hor_lines;
        Line * first_ver_lines;
        Line * temp_hor_lines;
        Line * temp_ver_lines;
        Line the_first_line_for_hor;
        Line the_first_line_for_ver;
        int n,m;
        string direction;
        string standard = "row";
        int on_lights_numbers = 0;
    };
    Light lights(nn,mm);
    while (lights.time < q )
    {
        lights.Create_new_line();
        cout << lights.calculate_and_report_lights();
        lights.time++;
    }
    return 0;
}

类和结构体声明放到main外面。
构造李对结构体指针初始化,point没有初始化,使用里面的former就报错了。

【以下回答由 GPT 生成】

这个问题是由于访问了空指针导致的。在你的代码中,你定义了一些结构和类,并在主函数中创建了一个Light对象。然而,在创建Light对象的构造函数中,你没有对point成员进行初始化,因此它被设置为了nullptr。所以当你尝试访问这个空指针时,程序崩溃并显示退出代码-1073741819。

要解决这个问题,你需要在创建point成员时为其分配内存。你可以使用new关键字在堆上为其分配内存。下面是修改后的代码:

class Light
{
public:
    Light(int n1, int m1)
    {
        this->n = n1;
        this->m = m1;
        this->hor_lines = new Line;
        this->ver_lines = new Line;
        this->first_hor_lines = this->hor_lines;
        this->first_ver_lines = this->ver_lines;
        this->hor_lines->point = new Point; // 为point成员分配内存
        this->ver_lines->point = new Point; // 为point成员分配内存
        this->hor_lines->point->former = nullptr;
        this->ver_lines->point->former = nullptr;
        this->hor_lines->point->latter = nullptr;
        this->ver_lines->point->latter = nullptr;
        this->hor_lines->first_point = this->hor_lines->point;
        this->ver_lines->first_point = this->ver_lines->point;
    }
    // other member functions...
private:
    int n;
    int m;
    Line the_first_line_for_hor;
    Line the_first_line_for_ver;
    Line *hor_lines;
    Line *ver_lines;
    Line *first_hor_lines;
    Line *first_ver_lines;
};

通过这样的修改,你为point成员分配了内存,避免了空指针访问而导致的程序崩溃。

希望这个解决方案对你有帮助。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^