类之间的关系——继承和组合

Relations between classes - both inheritance and composition

本文关键字:继承 组合 关系 之间      更新时间:2023-10-16

我正在开发一款游戏《Snake》。我对类之间的关系有问题,我真的不明白为什么。

我有三个类:

  • Snake -蛇类。
  • 食物-蛇吃的食物
  • 对象-有成员:width, height, posX和posY

关系如下:Snake拥有来自Object类的无限数量的对象。每个对象都是蛇的块。因此在snake声明中我有:

Object **blocks; 

然后,在snake构造函数中,我为block创建了一个object数组。不用担心这部分,我已经测试了Snake,并让它顺利地运行了几个块。Snake不是主要问题。

然后我尝试为Food类做继承,只要我只使用头文件而不使用pcp -file:

//Header file for Food
#include "Object.h"
class Food : Object { ............ };

到目前为止还好,但是!,只要我写一行:#include "Food.h" for food.cpp并尝试编译,编译器就会在Snake中发现一个错误(!?)我有一个错误提示" error: "Object" is not the name on a type"

Object **blocks;

这是否意味着我不能使用类(对象)来继承和组合?

编辑:我有太多的代码,没有时间缩短所有的代码。下面是Object.h的代码(我没有object.cpp文件,因为还不需要):
#ifndef OBJECT_H
#define OBJECT_H
#include "stdafx.h"
#include "Snake.h"
class Object {
private:
    int posX;
    int posY;
    int height;
    int width;
public:
    //Get functions
    int getPosX() const { return this->posX; }
    int getPosY() const { return this->posY; }
    int getHeight() const { return this->height; }
    int getWidth() const { return this->width; }
    //Set functions
    void setPosX(int x) { this->posX = x; }
    void setPosY(int y) { this->posY = y; }
    void setHeight(int h) { this->height = h; }
    void setWidth(int w) { this->width = w; }
};
#endif //OBJECT_H
下面是Snake.h的代码:
#ifndef SNAKE_H
#define SNAKE_H
#include "Object.h"
#include "stdafx.h"
class Snake {
public:
    enum Direction { Left, Right, Up, Down };
private:
    Object **blocks;
    int nrOfBlocks;
    float speed;
    int frontBlock;
    Direction direction;
    sf::Image blockImg;
    sf::Sprite blockSprite;
public:
    Snake();
    ~Snake();
    //Get functions
    int getNrOfBlocks() const { return this->nrOfBlocks; }
    float getSpeed() const { return this->speed; }
    Direction getDirection() const { return this->direction; }
    sf::Image getBlockImg() const { return this->blockImg; }
    sf::Sprite getSprite() const { return this->blockSprite; }
    //Set functions
    void setNrOfBlocks(int nrOfBlocks) { this->nrOfBlocks = nrOfBlocks; }
    void setSpeed(float speed) { this->speed = speed; }
    void setDirection(Direction direction) { this->direction = direction; }
    void setImage(sf::Image image) { this->blockImg = image; }
    void setBlockSprite(sf::Sprite sprite) { this->blockSprite = sprite; }
    void move(int n);
    void newFrontBlock();
    void changeDir(Direction dir);
    sf::Sprite doSprite(int n);
};
#endif //SNAKE_H

下面是Food.h的代码:

#ifndef FOOD_H
#define FOOD_H
#include "Object.h"
#include "stdafx.h"
class Food : public Object {
private:
    int points;
    int timeExperation;
    sf::Image image;
    sf::Sprite sprite;
public:
    Food();
    int getPoint() const { return this->points; }
    int getTimeExperation() const { return this->timeExperation; }
    void setPoints(int points) { this->points = points; }
    void setTimeExperation(int timeExp) { this->timeExperation =  timeExp; }
};
#endif //FOOD_H

我希望没有太多的代码。它主要是不重要的成员变量和set- get-函数。如果你在这里找不到任何错误,那么我稍后会带来更多的错误。谢谢你的帮助!

找到错误。由于某种原因,可能是测试或错误,我不小心在Object.h中包含了#include "Snake.h",同时在Snake.h中包含了#include "Object.h"

现在不知道为什么你只能在另一个文件中包含一个文件,而不是,也不是,但是,我只是从Object.h中删除了#include "Snake.h",现在它工作了!最奇怪的是,我根本不明白为什么food.cpp的#include"Food.h"触发了这个错误。如果有人知道这个,请回复,我想从我的错误中学习。

无论如何,感谢所有回答的人!