C++编译时提示:error: ‘x’ does not name a type

其中一种可能是你在int main()以外进行了单独赋值。如下就会报错(error: ‘a’ does not name a type):

#include<cstdio>
int a;
a=3;
int main()
{
    printf("%d",a);
}

这操作不被编译器接受,也就是主函数外只允许声明不允许其他操作,其他都要在主函数以内。

所以在定义全局变量的时候要么在声明时赋值,要么在主函数内赋值。如下是允许的:

#include<cstdio>
int a=3;
int main()
{
    printf("%d",a);
}

#include<cstdio>
int a;
int main()
{
    a=3;
    printf("%d",a);
}

猜你喜欢

转载自blog.csdn.net/m0_46606140/article/details/106630315