error: ‘cout’ was not declared in this scope
C++ 編程時,使用 cout
、endl
時可能會遇到error: ‘cout’ was not declared in this scope
這樣的錯誤提示。
這是因爲 c++ 在使用時,每一塊內容涉及到一個命名空間(函數域)
可以通過以下兩種方式處理
1、
std::cout << "a" << std::endl;
std::
代表的是 cout
定義在std
空間當中,::
爲域操作符,
std::cout
的意思爲從std
空間當中,找到cout
的定義。
2、在代碼中聲明使用這個命名空間,這樣在編碼中就不用再加std::
using namespace std;