g++ 報錯 ‘string’ is not a type name,即使已 #include <iostream> 並 using namespace std;。原因是 <iostream> 不保證包含 <string>(VS 內部實作恰好包含,導致移植時才發現)。
#include <iostream>
#include <string> // 必須顯式包含
using namespace std;
C++ 標準規定各標頭檔獨立,切勿依賴間接包含。養成直接包含所需標頭檔的習慣,提升跨平台相容性。