如果直接將變數宣告在header file,當你的程式引用兩次以上時,經由編譯會發生錯誤的情況。
因此在程式第一次宣告變數後,其他地方則以extern方式宣告,告知編譯器在其他地方已經宣告了這個變數。
//MYFunction.cpp
| int count=0; |
//MYHeader.h
| extern int count; |
//MainFunction.cpp
|
#include "MYHeader.h" void main(){ int x=count; } |
文章標籤
全站熱搜
如果直接將變數宣告在header file,當你的程式引用兩次以上時,經由編譯會發生錯誤的情況。
因此在程式第一次宣告變數後,其他地方則以extern方式宣告,告知編譯器在其他地方已經宣告了這個變數。
//MYFunction.cpp
| int count=0; |
//MYHeader.h
| extern int count; |
//MainFunction.cpp
|
#include "MYHeader.h" void main(){ int x=count; } |