, :
namespace N1
{
enum MyEnum
{
b = 80
};
}
- .h, , , . " " : , ( .cpp). : , , (, N:: b) (=.cpp ).
. , , - , . : , int MyInt 40 80 : , ? , ( ) - , . .
Using enum was an easy way to avoid having to separate the declaration and definition in your case (since enum is not covered by the second version of the One Definition rule), but if you really need a (non-constant) global type int, you can achieve this as follows:
sample.h
namespace N1
{
extern int b;
}
sample1.cpp
#include "sample.h"
namespace N1
{
int b = 5;
}
void foo()
{
using namespace std;
cout<<N1:b<<endl;
}
sample2.cpp
#include "sample.h"
void bar()
{
using namespace std;
cout<<N1:b<<endl;
}
source
share