Reputation: 37
This is a console based Maze Game. The idea is to write the game class in a header file and use the class in the main thread. I am not sure if I am doing it right because I get an error. How would I include a header file in my code?
I am using Cloud9 so I don't know if there is a difference between Cloud9 and application software IDE. I am very new to C++, only been using it for some weeks (3-4), so I would like to know if what I am doing it right.
Here is how my code is structured:
This is 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);
}
};
This is MazeGame.cpp:
#include <iostream>
#include <MazeGame.h>
int main(){
Maze m = new Maze(16, 16);
return 0;
}
Both files are in the same directory. However, I am getting this error on the console:
/home/ubuntu/workspace/Maze/MazeGame.cpp:4:22: fatal error: MazeGame.h: No such file or directory
#include <MazeGame.h>
^
Upvotes: 2
Views: 944
Reputation: 1
The user-defined header file, the syntax to include in the .cpp
file is
#include "userdefined.h"
Upvotes: 0
Reputation: 3911
writing #include <>
is not necessary but by default for built-in but just for your compilers include directories.
so if you want to write this way and work then:
add the folder of your new header files to include of your compiler then you can write:
#include<MazeGame.h>
or you can copy this header file to include folder and it will work fine. an example when installing new library like openGL we copy header to include folder
and lib
files to lib folder.
by default include ""
is used telling the compiler that the file is in the current working directory.
#include <iostream>
so the compiler in this case searches for iostream in the current directory and if it doesn't find it it searches for it in compiler's all include folders (built-in and added directories)Upvotes: 0
Reputation: 6427
fatal error: MazeGame.h: No such file or directory
#include <MazeGame.h>
Compiler cannot find MazeGame.h
file on the search path.
The difference between #include "MazeGame.h"
and #include <MazeGame.h>
is the pathes where compiler searches for the header file. Cloud9 uses GCC compiler, which specifies include syntax this way:
#include <file>
It searches for a file namedfile
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 namedfile
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
So for user-defined headers you should use "file"
and in your code above: #include "MazeGame.h"
.
Upvotes: 1
Reputation: 73627
The includes should be:
#include <iostream> // <...> for standard headers
#include "MazeGame.h" // "..." for your own headers
The error comes from using <...>
for your own headers, which causes the compiler looking for your header in the wrong place.
You could also consider moving the member function definitions in the cpp file, leaving in the header only their signature declaration:
header file :
class Maze{
protected:
...
public:
Maze(int width, int height);
static void setMazeBlock(std::string value);
...
};
cpp file:
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
}
...
By the way, it's a good practice to make headers self sufficient, so to include in the header the other headers they rely on, without expecting that you'll do it in the cpp (so due to the std::string, an include of <string>
in the header would be advisable.
Upvotes: 2
Reputation: 314
Since your header file is user-defined, you should declare it with double quotes:
#include "MazeGame.h"
The way you were trying to declare it is the method you would use for built-in headers. For example:
#include <iostream>
Upvotes: 3