關於c++中y1,y0等變量名沖突問題
前言
今天在做題時,定義了一個名為y1的全局變量,運行后出現了意想不到的報錯。
error: 'int y1' redeclared as different kind of symbol
int y1;
^~
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\cmath:45,
from test-y1.cpp:2:
c:\mingw\include\math.h:273:24: note: previous declaration 'double y1(double)'
_CRTIMP double __cdecl y1 (double);
分析
從描述可以看出,是出現了變量重復定義的錯誤,但是我仔細查看了代碼並沒有重復定義的變量,繼續往下看報錯提示發現,變量y1和“cmath”庫中的y1產生了沖突,(震驚,變量還會和標准庫中產生沖突。。。。。),后又去網上查了資料發現確實是存在這樣的現像。打開math.h頭文件在第269(可能多有不同)開始存在以下定義:
_CRTIMP double __cdecl j0 (double);
_CRTIMP double __cdecl j1 (double);
_CRTIMP double __cdecl jn (int, double);
_CRTIMP double __cdecl y0 (double);
_CRTIMP double __cdecl y1 (double);
_CRTIMP double __cdecl yn (int, double);
所以在全局作用域中以上變量名均不可使用。
具體原因可自行百度。