首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何定义全局变量(初学者有关问题)

2012-02-10 
怎么定义全局变量(菜鸟问题)我现在想定义一个length变量,需要在main函数中给出大小,然后再多个子文件中使

怎么定义全局变量(菜鸟问题)
我现在想定义一个length变量,需要在main函数中给出大小,然后再多个子文件中使用,这应该怎么定义和使用?
#include <>
int   length;

void   out()
{cout < <length;

}
void   main()
{int   length=10;
}

这样定义没法用阿


[解决办法]
#include <>
int length;

void out()
{cout < <length;

}
void main()
{
length=10;
}

这样即可,不要在函数中再次定义了,另外建议全局变量命名时加个g,例如int g_length

[解决办法]
定义方法如楼上所说,在别的文件中使用这个全局变量的时候,只需再次声明即可:加extern
例如: extern int g_length;

热点排行