2

I'm trying to import a .h file I wrote, but I get the following error:

$ g++ HeartRate.cpp
In file included from HeartRate.cpp:3:
In file included from ./HeartRate.h:2:
./Date.h:4:7: error: redefinition of 'Date'
class Date {
      ^
./Date.h:4:7: note: previous definition is here
class Date {
      ^
1 error generated.

This is the file I'm trying to import:

#include <iostream>
#include <string>

class Date {
    private:
        //...
    public:
        //...
};
Jones
  • 1,154
  • 1
  • 10
  • 35
  • 8
    Does Date.h have an include guard? – aschepler Oct 03 '16 at 00:40
  • 2
    I guess you are including the header twice as the error indicates and that the header file lacks [include guards](https://en.wikipedia.org/wiki/Include_guard) which makes the header text substitution process include the same definition twice, which is not allowed. – jpw Oct 03 '16 at 00:42
  • Ah, I was importing it from both me HeartRate.h and HeartRate.cpp files. Thanks. – Jones Oct 03 '16 at 00:44
  • Make your headers self-contained (if you need to use it, including it is sufficient — even if no other headers have yet been included) and idempotent (so that if it gets included twice, it does no damage). – Jonathan Leffler Oct 03 '16 at 01:15

0 Answers0