CPU:酷睿雙核
RAM: 4 GB
OS: Windows 7的64位
IDE:代碼:: Blocks的16.0
編譯:TDM-GCC MinGW
IT水平: Newb試圖學習C++
。爲什麼有時std :: cout是不夠的,使用命名空間標準是必需的?
我用下面的代碼來測試編譯:
#include <iostream>
#include <string>
using namespace std;
auto add([](auto a, auto b){ return a+b ;});
auto main() -> int {cout << add("We have C","++14!"s);}
沒有錯誤。控制檯輸出「We have C++14!」確認編譯器符合C++編程語言ISO/IEC 14882:2014標準,即C++ 14。
然後我試着通過評論using namespace std;
並用std::cout
替換cout
來「優化」代碼。現在的代碼是這樣的:
#include <iostream>
#include <string>
//using namespace std;
auto add([](auto a, auto b){ return a+b ;});
auto main() -> int {std::cout << add("We have C","++14!"s);}
生成消息:
||=== Build: Release in c++14-64 (compiler: GNU GCC Compiler) ===|
C:\CBProjects\c++14-64\c++14-64-test.cpp||In function 'int main()':|
C:\CBProjects\c++14-64\c++14-64-test.cpp|5|error: unable to find string literal operator 'operator""s' with 'const char [6]', 'long long unsigned int' arguments|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
問題:
- 什麼在第二個程序導致錯誤?
- 在這種情況下,如何避免可怕的
using namespace std
?
編輯 ,我決定問這個問題的事實,這裏指的是,儘管我真誠的努力我一直未能解決的問題。問題的目的是找到答案。是的,我意識到 - 現在問題已經得到解答 - 解決方案避開了我,因爲我不知道什麼是「字符串文字操作符」,而GNU GCC編譯器沒有最適合初學者的友好錯誤消息。正如我在這個問題的「標題」中所說 - 我是一個新手。我在C++的開始。我剛剛安裝了Code :: Blocks和TDM-GCC MinGW編譯器,因爲我想要C++ 14以及製作32位和64位可執行文件的能力。這個問題得到如此詳盡的答案這一事實意味着這個問題本身值得關注,有些人認爲這個問題和答案可能對未來其他人有用。我是這個網站的新成員,每次downvote都會讓我難以提出答案。我的名譽下降了20%,因爲有人在之後回答了這個問題。
如果您必須倒胃口,您能否至少指出問題所在?謝謝!
這是對字符串常量,不'cout'的's'後綴。 '使用std :: operator「」s' – Ryan
@Ryan'使用命名空間std :: string_literals;'是更傳統的方法。 –