c++ - 试图在另一个头文件中的一个头文件中使用一个类

标签 c++ compiler-errors header-files

我有一个 weightedDirectedGraph类和 vertex类在他们自己的头文件 weightedDirectedGraph.h 中。就是这个:

#ifndef GRAPH
#define GRAPH

#include <iostream>
#include <string>
#include <vector>
#include <list>
#include "minHeapVertex.h"
using namespace std;

class vertex
{
public:
    string data;
    list<vertex *> neighbors;
    bool known;
    int distance, id;
    vertex * path;

    vertex(string x)
    {
        data = x;
    }
};

class weightedDirectedGraph
{
private:
    list<vertex *> vertexList;
    vector<vector<int> > edgeWeights;   //2D vector to store edge weights
    int idCount;

    weightedDirectedGraph()
    {
        idCount = 0;
    }

    vertex * findVertex(string s);
    void dijkstrasAlg(vertex * s);

public:
    void addVertex(string x);

    //adds bi-directional edges
    void addWeightedEdge(string x, string y, int weight);
};

#endif

我有一个 minHeapVertex minHeapVertex.h 文件中的类,该文件将用作 Dijkstra 算法中的优先级队列。这是文件:
#ifndef MIN_HEAP_VERTEX
#define MIN_HEAP_VERTEX

#include <iostream>
#include <vector>
#include "weightedDirectedGraph.h"
using namespace std;

class minHeapVertex
{
public:
    explicit minHeapVertex(int capacity = 100)
        :heapArray(capacity + 1), currentSize{ 0 } {}

    bool isEmpty() const
    {
        return (currentSize == 0);
    }

    vertex * getMinVertex() const;  //getting C2143 error here that says I'm missing a semi-colon before '*'. Doesn't make sense though.
    void insert(vertex * insertItem);
    void deleteMin();                   
    vertex * deleteAndReturnMin();      
    void makeEmpty()
    {
        currentSize = 0;
    }
    void decreaseKey(int index, int decreaseValue);
    void remove(int index);

private:
    void buildHeap();
    void percolateDown(int hole);

    vector<vertex *> heapArray;
    int currentSize;
};
#endif

我遇到了很多编译错误(第一个是 getMinVertex() 声明上的 C2143 错误),我认为这可能与尝试访问 minHeapVertex.h 中的 vertex 类有关。有人可以显示我做错了什么?已经做了几个小时,尝试向前声明顶点类,尝试删除一些包含“”,查找错误代码并更改了一些东西,但没有任何效果,最后只是一堆的错误。

最佳答案

问题:
OP 在 minHeapVertex.h 和 weightedDirectedGraph.h 之间存在循环依赖关系。
解决方案:
消除依赖。
minHeapVertex.h 定义 minHeapVertex。 minHeapVertex 需要顶点。
weightedDirectedGraph.h 定义了顶点和 weightedDirectedGraph。两者都不需要 minHeapVertex。
此时的三种可能:

  • 将顶点旋转到它自己的 vertex.h 头文件中。 minHeapVertex.h 和 weightedDirectedGraph.h 都包含 vertex.h 而不是彼此。
  • weightedDirectedGraph.h 不需要 minHeapVertex.h,所以去掉 #include "minHeapVertex.h"从 weightedDirectedGraph.h 打破循环。
  • class vertex; 的前向定义在 minHeapVertex.h 和删除 #include "weightedDirectedGraph.h"来自 minHeapVertex.h。

  • 解决方案 1 是首选。为顶点提供自己的标题可能会防止将来出现问题。 2 最容易实现。 3太傻了,不推荐。
    为什么循环依赖会阻止 minHeapVertex 看到顶点:
    为了使这更容易看到,我已经从头文件中删除了所有其他包含。
    这是我愚蠢的小test.cpp
    #include "weightedDirectedGraph.h"
    
    int main(int argc, char * argsv[])
    {
      return 0;
    }
    
    编译器会生成一个 test.cpp 的临时文件。然后它将开始解析,直到找到包含指令。包含的文件被复制粘贴到包含语句的临时文件中。所以临时文件看起来像这样:
    #define GRAPH
    
    #include "minHeapVertex.h"
    using namespace std;
    
    class vertex
    {
    public:
        string data;
        list<vertex *> neighbors;
        bool known;
        int distance, id;
        vertex * path;
    
        vertex(string x)
        {
            data = x;
        }
    };
    
    class weightedDirectedGraph
    {
    private:
        list<vertex *> vertexList;
        vector<vector<int> > edgeWeights;   //2D vector to store edge weights
        int idCount;
    
        weightedDirectedGraph()
        {
            idCount = 0;
        }
    
        vertex * findVertex(string s);
        void dijkstrasAlg(vertex * s);
    
    public:
        void addVertex(string x);
    
        //adds bi-directional edges
        void addWeightedEdge(string x, string y, int weight);
    };
    
    
    int main(int argc, char * argsv[])
    {
      return 0;
    }
    
    编译器进一步解析并看到 minHeapVertex.h 和复制粘贴的包含,所以你得到这个:
    #define GRAPH
    
    #define MIN_HEAP_VERTEX
    
    #include "weightedDirectedGraph.h"
    using namespace std;
    
    class minHeapVertex
    {
    public:
        explicit minHeapVertex(int capacity = 100)
            :heapArray(capacity + 1), currentSize{ 0 } {}
    
        bool isEmpty() const
        {
            return (currentSize == 0);
        }
    
        vertex * getMinVertex() const;  //getting C2143 error here that says I'm missing a semi-colon before '*'. Doesn't make sense though.
        void insert(vertex * insertItem);
        void deleteMin();
        vertex * deleteAndReturnMin();
        void makeEmpty()
        {
            currentSize = 0;
        }
        void decreaseKey(int index, int decreaseValue);
        void remove(int index);
    
    private:
        void buildHeap();
        void percolateDown(int hole);
    
        vector<vertex *> heapArray;
        int currentSize;
    };
    
    using namespace std;
    
    class vertex
    {
    public:
        string data;
        list<vertex *> neighbors;
        bool known;
        int distance, id;
        vertex * path;
    
        vertex(string x)
        {
            data = x;
        }
    };
    
    class weightedDirectedGraph
    {
    private:
        list<vertex *> vertexList;
        vector<vector<int> > edgeWeights;   //2D vector to store edge weights
        int idCount;
    
        weightedDirectedGraph()
        {
            idCount = 0;
        }
    
        vertex * findVertex(string s);
        void dijkstrasAlg(vertex * s);
    
    public:
        void addVertex(string x);
    
        //adds bi-directional edges
        void addWeightedEdge(string x, string y, int weight);
    };
    
    
    int main(int argc, char * argsv[])
    {
      return 0;
    }
    
    解析为 #include "weightedDirectedGraph.h" , 但幸运的是 GRAPH已定义,因此大多数 weightedDirectedGraph.h 都被排除在外。如果没有,weightedDirectedGraph.h 中的所有内容都将被重新定义,并且 minHeapVertex.h 将再次被一遍又一遍地包含,最终编译器会崩溃或告诉您用礼貌的错误消息将脏话删除。
    无论如何,我们已经可以在上面的代码跟踪中看到出了什么问题:minHeapVertex需要知道类型vertex ,但不会为另外 20 行左右定义。
    如果 test.cpp 写成
    #include "minHeapVertex.h"
    
    int main(int argc, char * argsv[])
    {
      return 0;
    }
    
    头文件将被包含在另一个顺序中并且它会被编译,给人一种错误的安全感,直到有一天你编写了一个首先包含 weightedDirectedGraph.h 的程序。换句话说,库一直工作到它不工作为止,并且您没有更改库的代码行。把你的头发拉出来玩得开心。
    避免循环依赖、循环引用和圆锯。这三个都可以让你很糟糕。
    转至 using namespace std;这个邪恶的小快捷方式将 std 命名空间中的所有内容都添加到全局命名空间中。如果您有一个名为 reverse 的函数,现在您必须处理与 std::reverse 的潜在重载冲突。标准库很大。有大量的函数、类和变量名只是渴望重载、覆盖和简单地践踏你的东西。
    但那是你的问题。
    using namespace std;在标题中使它成为每个人的问题。任何使用您的图形库的人都必须穿过雷区,除非他们仔细查看您的头文件并看到该声明,否则他们不会有丝毫线索。
    Longer discussion can be found here.要么明确命名所有内容(std::vector,std::string,...),要么只引入您需要并且知道不会与您的代码冲突的部分 using .例如:
    using std::vector;
    using std::string;
    
    不要把它放在你的标题中,否则有人可能会想知道为什么他们的自制 vector 会吓坏。可能不应该是自制载体,但你不能拯救所有人。

    关于c++ - 试图在另一个头文件中的一个头文件中使用一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32128122/

    相关文章:

    c - 从函数内部进行矩阵访问

    linux - arm-linux-gnueabi 跳过了 PWD 中的某些 header ?

    c++ - std::string 构造函数中的 ITERATOR LIST CORRUPTED

    c++ - std::string 是否以 '/0' 终止?

    c++ - FANN:使用从多个文件读取的数据训练 ANN 时发生内存泄漏

    xcode - 使用Xcode测试为Objective-C++代码库设置单元

    c - SGABIOS 编译错误

    c++ - 模板函数 "Subscript requires array type",但适用于较小的项目。为什么?

    c++ - DFS 和替换为 for_each

    c++ - C++编译设计:安全地扩展类