#include<bits/stdc++.h>
using namespace std;
int n;
struct a
{
struct b
{
void put()
{
printf("%d", n);
}
}y;
void put()
{
y.put();
}
}x;
int main()
{
n = 1, x.put();
return 0;
}
如果把本代码标 //??? 的部分注释,把注释的部分取消注释,会有以下报错:
[Error] invalid use of non-static data member 'a::n'
求助为什么报错?
还有求助解决方案:可否给每个结构体内部定义一个变量使得内部嵌套结构体也可以使用?
我实际代码结构体内部的 n 不同,所以 n 无法放到结构体外面作为全局变量。
虽然我直到可以把结构体 b 定义在外面,但是每次调用还要接口放个 n,有点烦。不希望评论给出了上句提到的解决方案。