1

I'm trying to inherit a certain class into two subclasses in C++. I want the subclasses to run side by side, but they both inherit the superclass entity.hpp:

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

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

Of course, when I do

#include "Npc.hpp"

#include "Human.hpp"

In the same file, I run into a problem because entity.hpp is included twice. How would I get around this?

EDIT: The .cpp files were a typo.

6
  • 1
    en.wikipedia.org/wiki/Include_guard Commented Nov 2, 2012 at 13:16
  • @ahenderson, you should post your comment as an anwser. Commented Nov 2, 2012 at 13:17
  • 1
    Why are you trying to #include .cpp files ? Commented Nov 2, 2012 at 13:18
  • @PaulR that was a typo in the question. Commented Nov 2, 2012 at 13:19
  • @georgesl too late now, but I deleted my comment because I realize he was including implementation (cpp) files, which is a big NO!. Commented Nov 2, 2012 at 13:19

4 Answers 4

6

Either use include guards in your headers, or the #pragma once directive (which is not as widely supported).

Sign up to request clarification or add additional context in comments.

Comments

2

You should use include guards in entity.hpp:

#ifndef ENTITY_HPP_
#define ENTITY_HPP_

// code

#endif

3 Comments

Do I need the trailing underscore?
The trailing underscore is a naming scheme some people use to reduce the risk of name clashes with other #define values. Avoid leading underscores, because system include files use them.
@Ken you don't need it. The point is to avoid clashes, for which it is better to use longer and more specific defines, say SOMEPROJECT_SOMENAMESPACE_ENTITY_H_.
1

Wrap the code in the header file like this:

#ifndef ENTITY_HPP
#define ENTITY_HPP

<body of entity.hpp goes here>

#endif

2 Comments

So I would put these definitions in entity.hpp?
Yes. The second time entity.hpp gets included, ENTITY_HPP is already defined and the content is ignored. Just make sure that the include guards all have unique names.
0

You should only include ".h" or ".hpp" files. If you use Visual Studio, add

#pragma once

To the top of your each header file. If you use other compiler, then

#ifndef MY_HEADER_FILE_NAME
#define MY_HEADER_FILE_NAME

class Human : public Entity
{}

#endif

5 Comments

The first sentence is wrong, you can include cpp files as well.
It's not stupid if done right. Bulk builds speed up compilation time considerably in some cases.
Suggest you change You can only include ".h" or ".hpp" files to: You should normally only include ".h" or ".hpp" files.
Well first of all, "if you use ..., add .... If you use ..., add ..." - No, use that technique that works for all compilers in the first place (which is include guards). That being said, #pragma once is understood by other compilers as well, but it's still a platform-dependent extension for something achievable as easily with a single platform-independent technique. Your answer suggests that #pragma once is the way to go in VS. Include guards are the way to go on each compiler, and #pragma once an alternative on many compilers.
@micnyk It's neither stupid nor wrong per se. Sure it can be used poorly, or even abused, but so can just about everything else. But it can also be used right and sensibly. Case in point, I have a system where a lot of C code is dynamically generated by a program and must be compiled on the fly. Using a master .c file full of #include statements for the few hundred auto-generated .c files and a bit of extra glue makes a lot more sense that compiling all those indepedently and then linking them together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.