将 head 与 nullptr 进行比较时:,抛出异常:读取访问冲突。这是 0x20。”
While comparing head to nullptr: ,,Exception thrown: read access violation. this was 0x20."
我正在尝试使用邻接表实现一个图形,但是在名为 ,AddGraphEdge" 的 A_List class 函数中,出现了一个问题,抛出异常:读取权限 violation.this 是 0x20。”当我执行 if(head==nullptr) 时,它出现在 list.cpp 函数 ,AddEdge" 中。我只是简单地检查 head 是否为 null。当我分配 head=[=49= 时就地删除此语句时] 它告诉我同样的问题。如果我只使用普通列表,它工作正常,我在 main 中创建一个列表对象并调用 AddEdge 函数。我不知道为什么在另一个中调用这个函数会导致问题。
Graph.h
class Graph
{
protected:
int _Vertices;
int _Edges;
int _Density;
public:
Graph() : _Vertices(0), _Edges(0), _Density(0) {};
Graph(const int& Vertices,const int& Edges, const int& Density) : _Vertices(Vertices), _Density(Density), _Edges(Edges) {};
void set_Vertices(const int& Vertices) { _Vertices=Vertices; };
int get_Vertices() { return _Vertices; };
void set_Edges(const int& Edges) { _Edges=Edges; };
int get_Edges() { return _Edges; };
virtual void DisplayGraph() = 0;
virtual void Get_Random_Graph() = 0;
};
Edge.h
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;
};
};
List.h
#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();
};
邻接_List.h
#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);
};
List.cpp
#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;
}
邻接_List.cpp
#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()
{
}
main.cpp
#include <iostream>
#include "Adjacency_List"
int main()
{
A_List Graph_list;
Graph_list.AddGraphEdge(4,3,2);
Graph_list.DisplayGraph();
return 0;
}
构造函数将 _Adj_List 设置为 nullptr:
A_List() : Graph() { _Adj_List = nullptr; };
现在它被取消引用并被使用(它仍然是 nullptr):
void A_List::AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
_Adj_List[StartVertice].AddEdge(StartVertice, EndVertice, weigth);
}
但这可能不是唯一的错误,因为它的代码相当混乱
我正在尝试使用邻接表实现一个图形,但是在名为 ,AddGraphEdge" 的 A_List class 函数中,出现了一个问题,抛出异常:读取权限 violation.this 是 0x20。”当我执行 if(head==nullptr) 时,它出现在 list.cpp 函数 ,AddEdge" 中。我只是简单地检查 head 是否为 null。当我分配 head=[=49= 时就地删除此语句时] 它告诉我同样的问题。如果我只使用普通列表,它工作正常,我在 main 中创建一个列表对象并调用 AddEdge 函数。我不知道为什么在另一个中调用这个函数会导致问题。
Graph.h
class Graph
{
protected:
int _Vertices;
int _Edges;
int _Density;
public:
Graph() : _Vertices(0), _Edges(0), _Density(0) {};
Graph(const int& Vertices,const int& Edges, const int& Density) : _Vertices(Vertices), _Density(Density), _Edges(Edges) {};
void set_Vertices(const int& Vertices) { _Vertices=Vertices; };
int get_Vertices() { return _Vertices; };
void set_Edges(const int& Edges) { _Edges=Edges; };
int get_Edges() { return _Edges; };
virtual void DisplayGraph() = 0;
virtual void Get_Random_Graph() = 0;
};
Edge.h
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;
};
};
List.h
#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();
};
邻接_List.h
#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);
};
List.cpp
#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;
}
邻接_List.cpp
#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()
{
}
main.cpp
#include <iostream>
#include "Adjacency_List"
int main()
{
A_List Graph_list;
Graph_list.AddGraphEdge(4,3,2);
Graph_list.DisplayGraph();
return 0;
}
构造函数将 _Adj_List 设置为 nullptr:
A_List() : Graph() { _Adj_List = nullptr; };
现在它被取消引用并被使用(它仍然是 nullptr):
void A_List::AddGraphEdge(const int& StartVertice, const int& EndVertice, const int& weigth)
{
_Adj_List[StartVertice].AddEdge(StartVertice, EndVertice, weigth);
}
但这可能不是唯一的错误,因为它的代码相当混乱