0

So, I'm working on a project and I have a header file simpleCommand.hh that I need to reference in two other headers. I have included simpleCommand.hh in command.hh and builtin.hh. Now, builtin.hh is included in command.cc which also includes command.hh, so g++ is telling me that the struct SimpleCommand is being defined twice. However, if I remove simpleCommand.hh include from builtin.hh, g++ tells me that SimpleCommand hasn't been defined when I use it in builtin.hh.

Here's the weird part: if after doing all this, I go into builtin.hh and re-add include "simpleCommand.hh", the program compiles fine. However, randomly if I try to compile at another point in the future, g++ will give me this error again and I have to start the process over.

Am I missing something here?

Andrew Graber
  • 177
  • 4
  • 12
  • 2
    You should have header guards in your header files, so that including them a second time is ignored. – Barmar Mar 04 '18 at 04:23
  • Use include guards in your header file. Learn them here https://stackoverflow.com/questions/12928919/including-a-header-file-twice-in-c – 273K Mar 04 '18 at 04:24
  • Sorry, I neglected to mention that all the header files have the `#ifndef #define ... #endif` – Andrew Graber Mar 04 '18 at 05:30

1 Answers1

0

you juste need to delete SimpleCommand.hh from command.hh because build.hh get it and command.hh herited to build.hh so command.hh will get SimpleCommand.hh twice. Or you can declare some of function of SimpleCommand.hh as virtual :)