[英]C++: undefined reference to const pointer when declaring as extern
編寫以下程序時出現編譯器錯誤:
//file1.cpp
int a,b,c,d;
int *const pa[4] = {&a, &b, &c, &d};
//file2.cpp
extern int *const pa[4];
編譯時它會刪除 file2.cpp 中的錯誤:
undefined reference to `pa'
如何使用常量指針定義該指針數組並在不同的源文件中使用它?
此致 :-)
默認情況下, const
變量具有內部鏈接。 要獲得外部鏈接,請將extern
添加到定義中:
extern int *const pa[4] = {&a, &b, &c, &d};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.