0

I have a header file:

#ifndef DATA_HPP
#define DATA_HPP

#include <cstdint>

namespace f {
namespace t {
namespace e {

uint32_t a = 99;

class Test
{
public:
    Test();
    ~Test();
};

}
}
}
#endif

With CPP file try.cpp:

#include "try.hpp"

namespace f {
namespace t {
namespace e {

Test::Test()
{
}
Test::~Test()
{
}

}
}
}

And the main file - main.cpp:

#include "try.hpp"

int main()
{
f::t::e::Test a;
}

But when I compile my code:

c++ --std=c++11 try.cpp main.cpp

I get below error:

(.data+0x0): multiple definition of `f::t::e::a'
try.cpp:(.data+0x0): first defined here

I already have the header file guard - so why the error is being reported and how can I fix it?

Programmer
  • 8,303
  • 23
  • 78
  • 162
  • 3
    Header guard only protects against multiple inclusion of header files in the same [*translation unit*](https://en.wikipedia.org/wiki/Translation_unit_(programming)). It doesn't protect against multiple definitions in different translation units. – Some programmer dude Sep 14 '18 at 07:47
  • Seems like the endif in CPP file was a TYPO issues - remove the same – Programmer Sep 14 '18 at 07:48
  • Now your include guard is missing the `#endif` completely – UnholySheep Sep 14 '18 at 07:49
  • Edited and added – Programmer Sep 14 '18 at 07:51
  • #define DATA_HPP is specific to a given translation unit where the header file is included. So, when the same header file is included in a different translation unit they are read and included. – shainu vy Sep 14 '18 at 07:58

0 Answers0