类之间的关系——继承和组合
Relations between classes - both inheritance and composition
我正在开发一款游戏《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"触发了这个错误。如果有人知道这个,请回复,我想从我的错误中学习。
无论如何,感谢所有回答的人!
- 混合组合和继承的C++问题
- C++基于策略的设计:继承与组合
- 工厂方法模式使用继承而抽象工厂模式使用组合如何
- vector_base继承与组合
- 如何在 c++ 中将多重继承与组合一起使用?
- 是否可以使用 Gtk::D rawingArea (GTKmm) 作为组合而不是继承?
- C++:树根应该使用继承而不是组合吗?
- 必要时违反组合而不是继承可以吗?
- 避免使用组合进行额外的堆分配(过度继承)
- 为什么我在使用组合而不是继承时得到 C4624(无法访问基类析构函数)
- 编译器如何在继承C++中进行组合
- 类继承/组合设计
- Qt和C++:双重继承与组合
- 使用组合而不是继承的方法转发(使用C++特性)
- 将继承更改为组合
- C++中组合关系的继承
- 为什么与组合相比,私有继承会增加有人破坏我的代码的概率
- 两个接口,多个继承组合成一个容器
- 运算符<<和继承/组合
- c++中创建多继承组合类的设计指南