在另一个 class 中使用 class

Use class in another class

我需要在其他 class 粒子中包含两个 classes:迷宫和屏幕。我需要他们实施 moveEast 操作。 Maze 是一个随机生成的迷宫,Screen 具有我绘制字符的屏幕分辨率。我正在使用 opengl 和 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();
};

错误信息是:

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

我的想法是,我在 main.cpp 中有这些名为 m(迷宫)、s(屏幕)和 p(pacman,这是另一个 class)的全局变量。我需要用类似的东西调用 moveEast 操作:

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

我也放了主文件的声明部分。

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[]){
...
}

如有任何想法,我们将不胜感激。

MazeScreen 都在 namespace RandomMaze 中,但 particle 不在。

因此,要么您需要将 particle 移动到该命名空间中,这样您就可以在没有任何限定的情况下引用它们,或者您需要在使用它们时限定名称 - RandomMaze::Maze,等等。