Register__ 2022-02-14 19:22 采纳率: 89.5%

# C++调试报错 has incomplete type

``````template<typename E,typename W>
class ListGraph{
class Edge;
class Vertex;
public:
//unordered_map<E,Vertex> vertices; //用来存放 顶点

ListGraph(){}
~ListGraph(){}
int VertexSize();
int EdgeSize();
void removeVertex(E element);
void removeEdge(E form,E to);
};

template<typename E,typename W> class ListGraph<E,W>::Edge{
Vertex m_from,m_to;
W m_weight;
Edge(Vertex from,Vertex to,W weight):m_from(from),m_to(to),m_weight(weight){}
~Edge(){}
};
template<typename E,typename W> class ListGraph<E,W>::Vertex{
E m_element;
unordered_multiset<Edge >m_inEdges,m_outEdges;
Vertex(E element):m_element(element){}
~Vertex(){}
};

``````

``````unordered_map<E,Vertex> vertices; //用来存放 顶点
``````

• 写回答

#### 1条回答默认 最新

• _GX_ 2022-02-14 19:32
关注

你这里Edge和Vertex类相互依赖，只能通过指针来实现

``````#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <memory>
#include <vector>

using namespace std;

template <typename E, typename W>
class ListGraph
{
class Edge;
class Vertex;

public:
unordered_map<E, unique_ptr<Vertex>> vertices; //用来存放 顶点
vector<unique_ptr<Edge>> edges;

ListGraph() {}
~ListGraph() {}
int VertexSize();
int EdgeSize();
void addEdge(E from, E to, W weight);
void removeVertex(E element);
void removeEdge(E form, E to);
};

template <typename E, typename W>
class ListGraph<E, W>::Edge
{
public:
Vertex *m_from, *m_to;
W m_weight;
Edge(Vertex *from, Vertex *to, W weight) : m_from(from), m_to(to), m_weight(weight) {}
~Edge() {}
};

template <typename E, typename W>
class ListGraph<E, W>::Vertex
{
public:
E m_element;
unordered_multiset<Edge *> m_inEdges, m_outEdges;
Vertex(E element) : m_element(element) {}
~Vertex() {}
};

int main()
{
ListGraph<string, int> gp;
return 0;
}
``````
本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论 编辑记录

• 系统已结题 2月22日
• 已采纳回答 2月14日
• 创建了问题 2月14日

#### 悬赏问题

• ¥15 如何删除这个虚拟音频
• ¥50 hyper默认的default switch