C++ 将磁头与nullptr进行比较时:,引发异常:读取访问冲突。这是0x20。”;

C++ 将磁头与nullptr进行比较时:,引发异常:读取访问冲突。这是0x20。”;,c++,graph,adjacency-list,nullptr,C++,Graph,Adjacency List,Nullptr,我试图使用邻接列表实现一个图形,但在名为AddGraphEdge的a_list类函数中,出现了一个问题,引发了异常:读取访问冲突。这是0x20。如果(head==nullptr),则在list.cpp函数中,AddEdge中会出现。我只是简单地检查head是否为null。当我在指定head=new_edge时就地删除此语句时,它告诉我同样的问题。如果我只使用普通列表,并且我在main中创建一个列表对象并调用ADEDGE函数,则效果很好。我不知道为什么在另一个函数中调用此函数会导致问题 图h E

我试图使用邻接列表实现一个图形,但在名为AddGraphEdge的a_list类函数中,出现了一个问题,引发了异常:读取访问冲突。这是0x20。如果(head==nullptr),则在list.cpp函数中,AddEdge中会出现。我只是简单地检查head是否为null。当我在指定head=new_edge时就地删除此语句时,它告诉我同样的问题。如果我只使用普通列表,并且我在main中创建一个列表对象并调用ADEDGE函数,则效果很好。我不知道为什么在另一个函数中调用此函数会导致问题

图h
Edge.h
清单h
邻接列表
List.cpp
#包括“List.h”
#包括
无效列表::添加(常量整数和起始高度、常量整数和结束高度、常量整数和高度)
{
边*新边=新边;
新建边->设置边值(起点、终点、高度);
if(head==nullptr)
{
头部=新边缘;
}
其他的
{
边缘*tmp=头部;
头部=新边缘;
新建边->设置下一步(tmp);
}
ListSize++;
}
无效列表::显示列表()
{
边缘*tmp=头部;
while(head->get_NextEdge()!=nullptr)
{
std::无法获取开始时间()获取重量()获取结束时间()获取下一个日期();
}

std::cout get_StartVertice()get_Weight()get_EndVertice()构造函数将_Adj_List设置为null ptr:

A_List() : Graph() { _Adj_List = nullptr; };
现在它被取消引用并被使用(它仍然是nullptr):


但它可能不是唯一的错误,因为它的代码相当混乱,无法编译…发生了什么?我可以毫无问题地编译它,也许我没有在这里给出一些函数?a.cpp:(.text.startup+0x2e):未定义对
vtable的引用a.cpp:(.text.startup+0x54):未定义对
a_List::AddGraphEdge的引用(int-const&,int-const&,int-const&)a.cpp:(.text.startup+0x5e):对'a_List::DisplayGraph()的未定义引用“基本上,与列表相关的任何内容都会丢失。我很抱歉……我无意中插入了列表代码而不是邻接列表……现在应该可以了。只有邻接列表.cpp是错误的。对不起……这并不能解决问题,但名称以下划线开头,后跟大写字母(
\u顶点
\u边
\u密度
\u起点
\u终点
,\u下一步`
\u调整列表
)保留供实现使用。不要在代码中使用它们。非常感谢,它现在可以工作了。可以给我一些提示为什么这是一个凌乱的代码吗?我想尽可能地改进自己。@77jt777好吧,例如,通过常量引用传递int到方法-为什么?通过副本传递int更好(它开始使用更大的类型或对象)还有为什么不使用标准库std::list/std::vector?另一个是使用std::unique_ptr来保护内存。你知道,三/五规则、RAII…以及const正确性可以应用于类方法,它们不应该改变任何东西。例如:
int get_Edges()const{return _Edges;};
-即使在const实例上调用了它,也可以使用它。
class Edge
{
private:

    int _StartVertice;
    int _EndVertice;
    int _weight;
    Edge* _NextEdge;

public:

    Edge():_StartVertice(0), _EndVertice(0), _weight(0), _NextEdge(nullptr) {};

    Edge(const int& StartVertice, const int& EndVertice, const int& weight) : _StartVertice(StartVertice), _EndVertice(EndVertice), _weight(weight), _NextEdge(nullptr) {};

    const int& get_StartVertice() const { return _StartVertice; };

    const int& get_EndVertice() const { return _EndVertice; };

    const int& get_Weight() const { return _weight; };

     Edge* get_NextEdge()  { return _NextEdge; };

    void set_NextEdge(Edge* NextEdge) { _NextEdge = new Edge;  _NextEdge = NextEdge; };

    void set_EdgeValues(const int& StartVertice, const int& EndVertice, const int& weight)
    {
        _StartVertice = StartVertice;
        _EndVertice = EndVertice;
        _weight = weight;
    };
                

};
#include "Edge.h"



class List
{
private:

    int ListSize;
    Edge* head;

public:

    ~List() {};

    List() : ListSize(0), head(nullptr) {};

    const Edge* get_head() { return head; };

    const int get_ListSize() { return ListSize; };

    void AddEdge(const int& StartVertice, const int& EndVertice, const int& weigth);

    void DisplayList();


};
   #include "List.h"
    #include "Graph.h"
    
    
    class A_List: public Graph
    {
    private:
        List* _Adj_List;
    public:
    
        A_List() : Graph() { _Adj_List = nullptr; };
    
        A_List(const int& Vertices, const int& Edges, const int& Density) : Graph(Vertices, Edges, Density) { _Adj_List = new List[Vertices]; };
    
        virtual void DisplayGraph() override;
    
        virtual void Get_Random_Graph() override;
    
        void AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth);

};
#include "List.h"
#include <iostream>


void List::AddEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
    Edge* new_edge = new Edge;

    new_edge->set_EdgeValues(StartVertice, EndVertice, weigth);

    if (head == nullptr)
        {
            head = new_edge;
        }

    else
        {
            Edge* tmp = head;
            head = new_edge;
            new_edge->set_NextEdge(tmp);
        }

    ListSize++;
}

void List::DisplayList()
    {
    Edge* tmp = head;

    while (head->get_NextEdge() != nullptr)
        {
            std::cout << tmp->get_StartVertice() << tmp->get_Weight() << tmp->get_EndVertice() << std::endl;
            tmp = tmp->get_NextEdge();
        }
    std::cout << tmp->get_StartVertice() << tmp->get_Weight() << tmp->get_EndVertice() <<  std::endl;
    }
#include "Adjacency_List.h"

    void A_List::AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
    {
        _Adj_List[StartVertice].AddEdge(StartVertice, EndVertice, weigth);
    }
    
    
    
     void A_List::DisplayGraph()
    
    {int Vertices = A_List::get_Edges();
    
         for (int i = 0; i < Vertices; i++)
         {
             _Adj_List[i].DisplayList();
         }
    
    }
    
     void A_List::Get_Random_Graph() 
    {
    
    
    
    }
#include <iostream>
#include "Adjacency_List"



int main()
{

    A_List Graph_list;

    Graph_list.AddGraphEdge(4,3,2);

    Graph_list.DisplayGraph();


    return 0;
}
A_List() : Graph() { _Adj_List = nullptr; };
void A_List::AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
    _Adj_List[StartVertice].AddEdge(StartVertice, EndVertice, weigth);
}