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

C++ 在C+;中包含头文件的更好方法是什么+;密码?,c++,class,compiler-errors,header-files,C++,Class,Compiler Errors,Header Files,这是一个基于控制台的迷宫游戏。其思想是在头文件中编写游戏类,并在主线程中使用该类。我不确定我是否做对了,因为我出错了。如何在代码中包含头文件 我正在使用Cloud9,所以我不知道Cloud9和应用软件IDE之间是否有区别。我对C++非常陌生,只是使用了几周(3-4),所以我想知道我做的是不是正确。 下面是我的代码的结构: 这是MazeGame.h: #ifndef MAZEGAME_H #define MAZEGAME_H class Maze{ protected: int wid

这是一个基于控制台的迷宫游戏。其思想是在头文件中编写游戏类,并在主线程中使用该类。我不确定我是否做对了,因为我出错了。如何在代码中包含头文件

我正在使用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);
    }
};
\ifndef MAZEGAME\H
#定义MAZEGAME_H
班级迷宫{
受保护的:
整数宽度;
内部高度;
串迷宫;
公众:
迷宫(整数宽度,整数高度){
这个->宽度=宽度;
这个->高度=高度;
}
静态void setMazeBlock(标准::字符串值){
此->迷宫+=值;
}
无效数据块(整数集){
此->迷宫[设置]-=此->迷宫[设置];
}
std::string getMazeDrawing(){
返回此->迷宫;
}
void setMazeDrawing(std::string val){
这个->迷宫=val;
}
void drawMaze(整数倍=1){
对于(int i=0;i宽度;+i){
此->设置mazeblock(“#”);
}
此->设置mazeblock(std::endl);
对于(int i=0;iwidth;++i){
此->设置mazeblock(“#”);
对于(int j=0;jheight;++j){
此->设置mazeblock(std::endl);
如果(j==此->宽度){
此->设置mazeblock(“#”);
}
}
}
对于(int i=0;iwidth;++i){
此->设置mazeblock(“#”);
}
此->设置mazeblock(std::endl);
}
};
这是MazeGame.cpp:

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

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

    return 0;

}
#包括
#包括
int main(){
迷宫m=新迷宫(16,16);
返回0;
}
两个文件位于同一目录中。但是,我在控制台上遇到以下错误:

/home/ubuntu/workspace/Maze/MazeGame.cpp:4:22: fatal error: MazeGame.h: No such file or directory
#include <MazeGame.h>
                   ^
/home/ubuntu/workspace/Maze/MazeGame.cpp:4:22:致命错误:MazeGame.h:没有这样的文件或目录
#包括
^

包括:

#include <iostream>       // <...>  for standard headers
#include "MazeGame.h"     // "..."  for your own headers
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
}
...

顺便说一句,这是一个很好的做法,因此在标题中包含他们所依赖的其他标题,而不期望您在cpp中这样做(因此,由于std::string,建议在头文件中包含

由于头文件是用户定义的头文件,您应该用双引号声明它:

#include "MazeGame.h"
您试图声明它的方式是用于内置标题的方法。例如:

#include <iostream>
#包括
致命错误:MazeGame.h:没有这样的文件或目录
#include

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

#include“MazeGame.h”
#include
之间的区别在于编译器搜索头文件的路径。Cloud9使用GCC编译器,它指定以下方式:

#include
它在系统目录的标准列表中搜索名为
file
的文件。您可以使用-I选项将目录前置到此列表

#include“file”
它首先在包含当前文件的目录中搜索名为
file
的文件,然后在quote目录中搜索,然后在与
相同的目录中搜索。您可以使用-iquote选项将目录前置到quote目录列表中

因此,对于用户定义的标题,您应该使用
“file”
,并且在上面的代码中:
#include“MazeGame.h”
写入
#include
不是必需的,但默认情况下,对于内置的,但仅对于编译器include目录

因此,如果你想以这种方式写作并工作,那么:

  • 将新头文件的文件夹添加到编译器的文件夹中,然后可以编写:

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

默认情况下,使用
include”“
告诉编译器该文件位于当前工作目录中

  • 您还可以这样写:
    #include
    ,因此在本例中,编译器在当前目录中搜索iostream,如果未找到,则在编译器的所有include文件夹(内置和添加的目录)中搜索iostream

对于内置标题,您应该使用
#include
,而
#include“XXX.h”用户定义的标题。<代码> >请删除您的<代码> -> >代码>符号。C++中不需要。C++语言可能是java。您可能需要将较大的方法定义移到源文件中,这将加快您的构建过程。IMO,您的头文件应该<代码>包含< <代码> >,因为您在类声明中使用了成员。我建议使用扩展的“HPP”来处理C++文件和“.h”对于C语言文件来说,当C语言文件与C++文件混合时,这就变得很重要了。如果OP将包含标题“代码> MZEGAME.H./CUDE”的文件夹添加到编译器的目录中?!!它会变好的。
如果这个答案包含对语言规范的引用,它会更有用。
#include<MazeGame.h>