Reputation: 31
I have this mouse and maze program where I have defined a separate class for the mouse and the maze. I have included in the header of each class the other classes header file. In the mouse one, if I pass by reference the maze (so function(maze& myMaze, mouse& myMouse) it checks out fine. If I do the same in the maze, it says is not a type of that class. How do I fix this?
from maze cpp:
void maze::initialize_map(mouse& myMouse) {} (error of previous mention)
from mouse cpp:
string mouse::get_movement(maze& myMaze, mouse& myMouse) {} (no error)
mouse header:
#ifndef MOUSE_H
#define MOUSE_H
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
#include "maze.h"
class mouse {
private:
int life, locationX, locationY, toLocX, toLocY, los_max_distance;
char los_direction;
bool sight;
public:
mouse(): life(50), locationX(0), locationY(0), toLocX(0), toLocY(0), los_max_distance(0), los_direction('0'), sight(false) {}
int get_life() const;
int get_locationX() const;
int get_locationY() const;
bool get_sight() const;
std::string get_movement(maze&, mouse&);
void set_life(int);
void set_location(int, int, maze&);
void set_sight();
void unset_sight();
void set_path(char);
void set_distance(char);
};
#endif /* MOUSE_H */
maze header:
#ifndef MAZE_H
#define MAZE_H
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stack>
#include "mouse.h"
class maze {
private:
static const int MAX_LENGTH = 20;
static const int MAX_WIDTH = 20;
char board[MAX_LENGTH][MAX_WIDTH];
char wall, open, mouse, cheese;
int lastIntersectionX, lastIntersectionY, locationMX, locationMY, locationCX, locationCY, last_room;
bool scent;
std::stack <int> lastDirection;
struct directions {
std::string direction[4], next, prev;
int longest, length[4];
} movement;
public:
maze(): wall('+'), open('.'), mouse('m'), cheese('c'), lastIntersectionX(0), lastIntersectionY(0), locationMX(0), locationMY(0), locationCX(0), locationCY(0), last_room(0), scent(false) {
for (int i = 0; i < 20; ++i) {
for (int k = 0; k < 20; ++k) {
board[i][k] = '0';
}
}
for (int i = 0; i < 4; ++i) {
movement.direction[i] = "none";
movement.length[i] = 0;
}
movement.next = "none", movement.prev = "none", movement.longest = 0;
};
void initialize_map(mouse&);
void view_map() const;
char get_map_room(char, char) const;
void set_map_room(int, int, char);
void line_of_sight(maze&, mouse&);
void get_exit(maze&, mouse&);
std::string get_next(maze&, mouse&) const;
std::string get_prev(maze&, mouse&) const;
int get_longest() const;
int get_mouse_locationX() const;
int get_mouse_locationY() const;
void view_location() const;
void set_mouse_location(int, int);
void set_cheese_location(int, int);
};
#endif /* MAZE_H */
NOTE: This is a lab for my class. However, this particular problem has nothing to do with it. THe professor tells me I'm making this harder than I have to, and I likely am, but I am enjoying doing this work in this way and I'd like to continue. If I can get these two to pass information I can move forward with my project.
Upvotes: 0
Views: 174
Reputation: 254721
You have a circular dependency; each header tries to include the other. The result is that one class definition will be included before the other, and won't know about the other class, hence the error.
In your case, mouse
doesn't need the full definition of maze
, only that the class exists. So remove the inclusion of the definition
#include "maze.h"
and instead just declare the class
class maze;
I think you can do the same in the other header.
Upvotes: 3
Reputation: 18431
You need to do one or more of:
class maze;
in mouse.h
header before mouse
class, OR the other way around.#include
required headers in CPP/H files. For example #include"maze.h"
in mouse.cpp
Upvotes: 2