Register__ 2022-02-14 19:22 采纳率: 89.5%
浏览 53
已结题

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 addVertex(E element);
    void addEdge(E from,E to,W weight);
    void addEdge(E from,E to);
    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; //用来存放 顶点

就报错了
错误 :

img

  • 写回答

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 addVertex(E element);
        void addEdge(E from, E to, W weight);
        void addEdge(E from, E to);
        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 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?