I'm new to using namespace, and im trying to print these values out with print but dont really know how to create the fuction print() to be able to print the values out
#include<iostream>
namespace X{
int var=0;
//void print(int var); trying define fuction called print()
}
namespace Y{
int var=0;
//void print(int var); trying define fuction called print()
}
namespace Z{
int var=0;
//void print(int var); trying define fuction called print()
}
int main()
{
X::var=7;
//std::cout<<X::var;
using namespace Y;
var=9;
print();
{
The using keyword is usually something done outside of functions. The fact that you have usingnamespace Y; followed by using Z::var; using Z::print; means that you are probably going to have some problems calling the function that you expect. One of the reasons that we use namespaces is so that we can write our own functions or declare global variables without giving them a totally global scope. That means that we can make a sqrt(double x) function of our own and use the one from <cmath> at the same time.
The using keyword is usually something done outside of functions.
I tend to disagree with this, using should be used in the smallest scope that is appropriate. The closer to global scope that it is used the more likely you are to run into conflicts.