-4

In Windows I can see any variable I declared in a namespace from outside of that namespace, but when it comes to Multi compiler, it is so strict, if you declared some variable in let say namespace X, you must access that variable only in namespace X, but I want so access this variable also from out of the namespace.

Is there any pre-processor definitions or any way to make this possible. It is a must for me, I cannot delete the namespaces.

Any kind of help is appreciated.

namespace X  
{
   int a = 5;
}

int b = a + 6;

result: a is undefined in multi compiler
Burak
  • 29
  • 7
  • Please, provide code for your questions, and errors if your compiler is complaining about anything. – Tomaz Canabrava Jun 20 '17 at 12:20
  • I would like to but this is a private project thats why I cannot provide code I can only give an example – Burak Jun 20 '17 at 12:22
  • I would like to but this is a private project that's why I cannot provide help I can only give random guesses. Please, write a minimal code that triggers your error, without it we cannot help because it would be guesswork. – Tomaz Canabrava Jun 20 '17 at 12:28
  • Tomaz thank you for your attention but what I wrote is the summary of the problem, If i need to provide code for the problem I have to commit 4-5 classes as a whole and I am not allow to do that, thanks again. – Burak Jun 20 '17 at 12:47

2 Answers2

2

GHS/Multi is right in rejecting your code (as is GCC, for example).
To access members of a namespace, use the scope resolution operator:

namespace X  
{
  int a = 5;
}

int main()
{
  int b = X::a + 6;
}
You
  • 22,800
  • 3
  • 51
  • 64
0

As an alternative to qualifying every use:

namespace X  
{
  int a = 5;
}
using X::a; // In the global namespace, a now means X::a

int main()
{
  int b = a + 6; // Per the using-declaration, this is X::a
}
MSalters
  • 173,980
  • 10
  • 155
  • 350