在 C++ 代码中包含头文件的更好方法是什么?

What is a better way of including header files in a C++ code?

这是一款基于主机的迷宫游戏。想法是在头文件中编写游戏class,并在主线程中使用class。我不确定我是否做对了,因为我收到了一个错误。如何在我的代码中包含头文件?

我用的是Cloud9,不知道Cloud9和应用软件有什么区别IDE。我是 C++ 的新手,只使用了几个星期 (3-4),所以我想知道我的做法是否正确。

我的代码结构如下:

这是MazeGame.h:

#ifndef MAZEGAME_H
#define MAZEGAME_H

class Maze{
protected: 
    int width;
    int height;
    std::string maze;

public:
    Maze(int width, int height){
        this->width = width;
        this->height = height;
    }

    static void setMazeBlock(std::string value){
        this->maze += value;
    }

    void destroyMazeBlock(int set){
        this->maze[set] -= this->maze[set];
    }

    std::string getMazeDrawing(){
        return this->maze;
    }

    void setMazeDrawing(std::string val){
        this->maze = val;
    }

    void drawMaze(int times = 1){

        for(int i = 0; i <= times; ++i){
            std::cout << this->maze;
        }
    }

    void generate(){
        for(int i = 0; i < this->width; ++i){
                this->setMazeBlock("#");
        }
        this->setMazeBlock(std::endl);
        for(int i = 0; i < this->width; ++i){
            this->setMazeBlock("#"); 
            for(int j = 0; j < this->height; ++j){
                this->setMazeBlock(std::endl);
                if(j == this->width){
                    this->setMazeBlock("#");
                }
            }
        }
        for(int i = 0; i < this->width; ++i){
                this->setMazeBlock("#");
        }
        this->setMazeBlock(std::endl);
    }
};

这是MazeGame.cpp:

#include <iostream>
#include <MazeGame.h>

int main(){
    Maze m = new Maze(16, 16);

    return 0;

}

两个文件都在同一个目录中。但是,我在控制台上收到此错误:

/home/ubuntu/workspace/Maze/MazeGame.cpp:4:22: fatal error: MazeGame.h: No such file or directory
#include <MazeGame.h>
                   ^

包含应该是:

#include <iostream>       // <...>  for standard headers
#include "MazeGame.h"     // "..."  for your own headers

错误来自于对您自己的 header 使用 <...>,这会导致编译器在错误的位置查找您的 header。

您还可以考虑移动 cpp 文件中的成员函数定义,在 header 中只留下它们的签名声明:

header 文件:

class Maze{
protected: 
    ...
public:
    Maze(int width, int height); 
    static void setMazeBlock(std::string value); 
    ...
};

cpp 文件:

Maze::Maze(int width, int height){
    this->width = width;
    this->height = height;
}
void Maze::destroyMazeBlock(int set){
    maze[set] -= maze[set];     // n need for this-> here
}
...

顺便说一句,制作 headers self sufficient 是一个很好的做法,因此将他们依赖的其他 header 包含在 header 中,不要期望你会这样做它在 cpp 中(因此由于 std::string,建议在 header 中包含 <string>

因为你的header文件是user-defined,你应该用双引号声明它:

#include "MazeGame.h"

您尝试声明它的方式是您将用于 built-in header 的方法。例如:

#include <iostream>

fatal error: MazeGame.h: No such file or directory #include <MazeGame.h>

编译器在搜索路径上找不到 MazeGame.h 文件。

#include "MazeGame.h"#include <MazeGame.h>的区别在于编译器搜索header文件的路径。 Cloud9 使用 GCC 编译器,它指定 include syntax 这样:

#include <file> It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option

#include "file" It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>. You can prepend directories to the list of quote directories with the -iquote option

所以对于 user-defined headers 你应该使用 "file" 并且在你上面的代码中:#include "MazeGame.h".

编写 #include <> 不是必需的,但默认情况下 built-in 但只是为了您的编译器包含目录。

所以如果你想这样写然后工作:

  • 添加新 header 文件的文件夹以包含在编译器中,然后您可以编写:

    #include<MazeGame.h>
    
  • 或者您可以将此 header 文件复制到 include 文件夹,它会正常工作。安装新库(如 openGL)时的示例我们将 header 复制到 include folder 并将 lib 文件复制到 lib 文件夹。

默认使用include ""告诉编译器该文件在当前工作目录中。

  • 你也可以这样写:#include <iostream> 所以在这种情况下,编译器会在当前目录中搜索 iostream,如果没有找到,它会在编译器的所有包含文件夹中搜索它 (built-in 并添加了目录)