所以,我基本上设计了一个"Hello,World!" OOP与c ++.我有两个类,鸡和狗继承公共动物.在int main中,当我创建每个的实例时,我得到的错误消息声称我创建了Animal类的多个实例.
Animal.h
#ifndef ANIMAL
class Animal
{
int x;
int y;
int z;
public:
void setPosition(int newX, int newY, int newZ);
void setX(int newX);
void setY(int newY);
void setZ(int newZ);
int getPosition();
int getX();
int getY();
int getZ();
};
#endif
Run Code Online (Sandbox Code Playgroud)
Chicken.h
#ifndef ANIMAL_H
#include "../animal.h"
#endif
class Chicken : public Animal
{
int id;
bool isClucking;
bool isEnraged;
public:
void setID(int newID);
void setClucking(bool yn);
void setEnraged(bool yn);
int getID();
bool getClucking();
bool getEnraged();
};
Run Code Online (Sandbox Code Playgroud)
Dog.h
#include "../animal.h"
class Dog : public Animal
{
int id;
public:
void setID(int newID);
int getID();
};
Run Code Online (Sandbox Code Playgroud)
代码:源代码
你将animal.h定义为
#ifndef ANIMAL
class Animal
{
Run Code Online (Sandbox Code Playgroud)
应该
#ifndef ANIMAL
#define ANIMAL
class Animal
{
Run Code Online (Sandbox Code Playgroud)
所以animal.h不包括多次.
同样为了保持一致性,让所有标题都包含一个包含警示.所以狗和鸡需要它.
除非你真的需要它
#ifndef ANIMAL_H
#include "../animal.h"
#endif
Run Code Online (Sandbox Code Playgroud)
应该只是
#include "../animal.h"
Run Code Online (Sandbox Code Playgroud)
因为前一个构造需要define完全匹配,因为你的原因已经不匹配了.animal.h定义ANIMAL或至少尝试但你要包括的是你要检查的地方ANIMAL_H
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |