C++ 在多个文件中包含一个文件

标签 c++ inheritance include c-preprocessor

我试图将某个类继承到 C++ 中的两个子类中。我希望子类并排运行,但它们都继承了父类(super class)entity.hpp:

#include "../entity.hpp"
class Npc : public Entity
{}

#include "../entity.hpp"
class Human : public Entity
{}

当然,当我这样做的时候

#include "Npc.hpp"

#include "Human.hpp"

在同一个文件中,我遇到了一个问题,因为 entity.hpp 被包含了两次。我该如何解决这个问题?

编辑:.cpp 文件是错字。

最佳答案

要么在你的 header 中使用 include guards,要么使用 #pragma once 指令(没有得到广泛支持)。

关于C++ 在多个文件中包含一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13195944/

相关文章:

java - 具有两个列表的类的组合和转发方法

Javascript原型(prototype)继承私有(private)方法

C++代码速度,哪个更快?

c++ - 面试问题 - 帮助不大

c++ - cout 一个带有类定义的变量

python - 在python中用*args和**kwds调用父类的函数

build-process - CMake 并包括其他 makefile

java - 如何从一个文件夹中单独获取 .png 文件

c++ - 恢复 wchar.h C++ 包含文件

c++ - reinterpret_cast<char *> 是 reinterpret_cast 的唯一有效用法吗?