-3

Maybe it is base knowledge, I am apologize.

I have a header file CTemp.h, with namespace CTemp:

namespace CTemp
{
    bool bFlag;

};

#ifndef _CTEMP
#define _CTEMP
bool CTemp::bFlag = true;

#endif 

and if I include CTemp.h to my cpp file and try to use CTemp::bFlag

bool bb = CTemp::bFlag;

the compiler throw error "redefinition"

I surely know, that I should to put initialization to a .cpp file, but I find some way, to solve it only with .h file. Because I don't want to add the .cpp file to my project. I thought, I could solve it with preprocessor directives #ifdef....

Thanks for advice.

Jason
  • 36,170
  • 5
  • 26
  • 60
Michal Hadraba
  • 347
  • 1
  • 11
  • 1
    why is the declaration outside of the include guards? – 463035818_is_not_an_ai Sep 21 '22 at 09:15
  • You can use `extern bool bFlag` in your H file (declaration), and `bool bFlag = true;` in the CPP (definition). However - using global variables is usually not a good idea (to say the least). – wohlstad Sep 21 '22 at 09:15
  • 1
    Because both `bool bFlag;` and `bool CTemp::bFlag = true;` are definitions. – Jason Sep 21 '22 at 09:15
  • 1
    Dupe: [How can I call a method of a variable, which contains in a namespace?](https://stackoverflow.com/questions/72569547/how-can-i-call-a-method-of-a-variable-which-contains-in-a-namespace/72569621#72569621) and [Global Constants in .h included in multiple c++ project](https://stackoverflow.com/questions/72355478/global-constants-in-h-included-in-multiple-c-project/72357013#72357013) – Jason Sep 21 '22 at 09:28

2 Answers2

3

In the header file you should only declare the variable:

#ifndef CTEMP_H
#define CTEMP_H

namespace CTemp
{
    extern bool bFlag;  // Need extern to only declare the variable
}

#endif // CTEMP_H

Then in one single source file you define the variable with its initialization:

#include "ctemp.h"

bool CTemp::bFlag = true;

Alternatively define the variable as inline:

#ifndef CTEMP_H
#define CTEMP_H

namespace CTemp
{
    inline bool bFlag = true;
}

#endif // CTEMP_H

Now you should not define the variable in a source file.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • But inline bFlag will not be global, isn't it? – Michal Hadraba Sep 21 '22 at 10:02
  • 1
    @MichalHadraba Who said that? With C++17, you can use `inline` and `bFlag` will still be global as shown in this answer. Also the exact same thing is explained here: [How can I call a method of a variable, which contains in a namespace?](https://stackoverflow.com/a/72569621/12002570) in the dupe. See *method 2* of the above linked [answer](https://stackoverflow.com/a/72569621/12002570). – Jason Sep 21 '22 at 10:29
0

The problem is that both bool bFlag and bool CTemp::bFlag = true; are definitions. In other words, you're defining bFlag for the second time when you wrote: bool bFlag = true;.

namespace CTemp
{
    bool bFlag;  //this is a definition for the first time

};

#ifndef _CTEMP
#define _CTEMP
bool CTemp::bFlag = true;  //this is also a definition but for the second time and hence the error

Solution

To solve this you can use the extern keyword:

someheader.h

#ifndef _CTEMP
#define _CTEMP
namespace CTemp
{
    extern bool bFlag; //this is a declaration and not a definition

};
#endif

somesource.cpp

#include "someheader.h"
bool CTemp::bFlag = true; //this is a definition
Jason
  • 36,170
  • 5
  • 26
  • 60