Search code examples
c++classopenglglut

Use class in another class


I need to include two classes: Maze and Screen inside other class particle. I need them to implement the moveEast operation. Maze is a random generated maze and Screen has the resolution of the screen which i'd draw the characters. I'm using opengl and glut.

ghost.h

#ifndef GHOST_H_
#define GHOST_H_
#include "Particle.h"

namespace RandomMaze {

class Ghost : public particle {
public:
    Ghost();
    virtual ~Ghost();
    void drawGhost();

};

} /* namespace RandomMaze */
#endif /* GHOST_H_ */

ghost.cpp

#include "Ghost.h"
#include "Particle.h"

namespace RandomMaze {

Ghost::Ghost() {
    // TODO Auto-generated constructor stubparticle::particle()

      state=QUIET;
}


Ghost::~Ghost() {
    // TODO Auto-generated destructor stub
}

void Ghost::drawGhost()
{
  glColor3f(1,1,1);
  glBegin(GL_QUADS);
  glVertex2i(x-6,y-6);
  glVertex2i(x+6,y-6);
  glVertex2i(x+6,y+6);
  glVertex2i(x-6,y+6);
  glEnd();
}

} /* namespace RandomMaze */

maze.h

#ifndef MAZE_H_
#define MAZE_H_
#include <stdlib.h>
#include <time.h>
#include <set>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdexcept>
#include <GL/glut.h>

using namespace std;

//Definimos los posibles estados de una coordenada: WALL ( pared), PASSAGE(pasillo) o VISITED ( visitado)
#define WALL 0
#define PASSAGE 1
#define FOOD 2
#define VISITED 9
#define RATIO 0.4

//Personajes
#define PACMAN 10 //Comecocos, amarillo

#define BLINKY 11  //Fantasma Rojo ( cazador)
#define PINKY 12  //Fantasma Rosa( emboscador)
#define INKY  13 //Fantasma Azul, cian ( caprichoso)
#define POKEY  14  //Fantasma Naranja ( bobo)




namespace RandomMaze {

/** Esta clase genera y gestiona el laberinto del juego. */
class Maze {

        //Atributos publicos
    public:
        int **map;
        int rows, columns;
        int level;

    //Metodos privados
    private:
        void fillMaze();
        void addBorders();
        void centerWalls();
        void addWalls();
        void exploreMaze(int fila, int columna);
        void checkWalls(int fila, int columna);
        void checkWalls();
        bool isConnected();
        bool isCenter(int r, int c);
        void getaway();
        int isWall(int level);
        void deleteBrick(int i, int j);
        void addFood();
        void addPacman();
        void addBlinky();
        void addPinky();
        void addInky();
        void addPokey();

    //Metodos públicos
    public:
        Maze();
        ~Maze(void);
        Maze(int filas, int columnas, int level);
        double getN();
        void setupMaze(int filas, int columnas, int level);
        void moveGhost(int fila, int columna);
        void printMaze();
        void setRows(int filas);
        void setColumns(int columnas);
        void setLevel(int nivel);
        int getRows();
        int getColumns();
        int getLevel();
        int** getMaze();
        // Pacman
        int getPacmanX();
        int getPacmanY();
        void setPacman( int x,int j);

        //Blinky
        int getBlinkyX();
        int getBlinkyY();
        void setBlinky( int x,int j);

        //Pinky
        int getPinkyX();
        int getPinkyY();
        void setPinky( int x,int j);

        //INKY
        int getInkyX();
        int getInkyY();
        void setInky( int x,int j);

        //POKEY
        int getPokeyX();
        int getPokeyY();
        void setPokey( int x,int j);

        void drawMaze(int width, int height);
        bool isPassageOrFood( int x, int y);
        void deleteFood(int x,int y);

};


} /* namespace RandomMaze */

#endif /* MAZE_H_ */

screen.h

#ifndef SCREEN_H_
#define SCREEN_H_

namespace RandomMaze {

class Screen {
    int width;
    int height;
public:
    Screen();
    Screen(int w, int h);
    int getWidth();
    int getHeight();
    virtual ~Screen();
};

} /* namespace RandomMaze */
#endif /* SCREEN_H_ */

particle.h

#include "Maze.h"
#include "Screen.h"

class particle {

protected: 

  int i,j; // Current position in map
  float x,y;   //-- Current position
  float vx,vy; //-- Velocity vector
  int state;

  long time_remaining;

public:

  particle();
  void set_positionScreen(int x,int y);
  void set_positionMap(int i,int j);
  int get_positionMapI();
  int get_positionMapJ();
  void moveEast(Maze m, Screen s);
  void init_movement(int destination_x,int destination_y,int duration);
  void init_movementAlongX(int destination_x,int destination_y,int duration);
  void init_movementAlongY(int destination_x,int destination_y,int duration);
  void integrate(long t);
  int getState();
  void draw();
  virtual ~particle();
};

The error message is:

    ../Particle.h:44:25: error: ‘Screen’ has not been declared
       void moveEast(Maze m, Screen s);
                         ^
make: *** [Ghost.o] Error 1

The idea is that i have these global variables called m ( maze), s (Screen) and p ( pacman, which is another class), inside the main.cpp. Id need to call the moveEast operation with something like:

Maze m;
Screen s;
Pacman p;
p.moveEast(m,s); 

Also i put the declaration part of the main file.

main.cpp

#include "Maze.h"
#include "Screen.h"
#include "Particle.h"
#include <vector>
#include "Ghost.h"
#include "Pacman.h"

#define ROWS 19
#define COLUMNS 40
#define LEVEL 5

#define WIDTH 600
#define HEIGHT 400

#define WINDOW_X 50
#define WINDOW_Y 50

//THE TIME THAT CHARACTER TAKES FROM ONE "SCREEN UNIT TO ANOTHER". Pacman is slightly faster than ghosts.
#define PACMAN_SPEED 1
#define GHOST_SPEED 1000

//Se puede utilizar para correccion
#define CORRECTION 6

void display();
void keyboard(unsigned char c,int x,int y);
void specialkeyboard(int key,int x,int y);
void idle();
void goNorth();
void goWest();
void goEast();
void goSouth();


using namespace std;
using namespace RandomMaze;


//Definición de variables.

vector<Ghost> playingGhosts;
Maze m;
Screen s(WIDTH, HEIGHT);
Pacman myPacman;
long last_t=0;

int main (int argc, char *argv[]){
...
}

Any ideas would be apreciated.


Solution

  • Both Maze and Screen are in namespace RandomMaze, but particle is not.

    So either you need to move particle into that namespace, so you can refer to them without any qualification, or you need to qualify the names when you use them - RandomMaze::Maze, etc.