[英]Accessing one namespace data member into another
How do I access the y (which is inside namespace n2), from n1 namespace: The test code is below: 如何从n1命名空间访问y(位于n2命名空间内):测试代码如下:
#include<iostream>
using namespace std;
namespace n1
{
int x = 20;
int m = ::n2::y;
void printx()
{
cout << "n1::x is " << x << endl;
cout << "n2::y is " << m << endl;
}
}
namespace n2
{
int y = 10;
}
int main()
{
cout << n1::x << endl;
n1::printx();
cout << n2::y << endl;
return 0;
}
I am getting below error: test.cpp:7:15: error: '::n2' has not been declared int m = ::n2::y; 我得到以下错误:test.cpp:7:15:错误:':: n2'尚未声明int m = :: n2 :: y;
Just change the order, so that n2 is resolvable inside n1 : 只需更改顺序,即可在n1内解析n2:
#include<iostream>
using namespace std;
namespace n2
{
int y = 10;
}
namespace n1
{
int x = 20;
int m = n2::y;
void printx()
{
cout << "n1::x is " << x << endl;
cout << "n2::y is " << m << endl;
}
}
int main()
{
cout << n1::x << endl;
n1::printx();
cout << n2::y << endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.