1
#include <iostream>
using namespace as std;
int x;
x=10;
int main()
{
cout<<x<<endl;
return 0
}
聲明全局變量,這給出了一個錯誤,但如果我使用:的定義和C++
int x=10;
代替:
int x;
x=10;
它工作正常。 任何人都可以指出這個問題嗎?編譯器讀取錯誤:
expected constructor, destructor, or type conversion before '=' token compilation terminated due to -Wfatal-errors.
如果我寫int x; X = 10;在main()內部,那麼它的工作原理。所以你的意思是你上面說的東西只適用於全局變量。 –
是的,它只適用於全局變量。只有全局變量可以在main()或其他函數的主體之外聲明。 –