[問題] #include <string> 但是 strlen 沒有

看板C_and_CPP (C/C++)作者 (huggie)時間16年前 (2010/06/15 14:29), 編輯推噓3(307)
留言10則, 5人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 要編譯別人寫的軟體 但是發現對方有 #include <string> 但是 strlen 等 string 相關函式都沒辦法呼叫到 太久沒碰 C++ 了..是跟 namespace 有關係嗎? 我為了 compile 直接多加 #include <string.h> 這樣已經解決問題了,但覺得很奇怪為什麼不能 compile 是需要 using namespace std; 等的嗎? 為何原始作者不會遇到相同問題呢 希望得到的正確結果: 正確 compile (我已經 compile 成功了 只是仍想要提問) 程式跑出來的錯誤結果: 就是沒有 include 到或者用到 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Linux -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 120.126.38.177 ※ 編輯: huggie 來自: 120.126.38.177 (06/15 14:30)

06/15 14:30, , 1F
strlen() 是 C 的 function,所以是 string.h 呀
06/15 14:30, 1F

06/15 14:31, , 2F
#include <string> 是 C++ 的 String class...
06/15 14:31, 2F

06/15 14:34, , 3F
至於原因請參考本板文章 #19ildeDh
06/15 14:34, 3F

06/15 14:37, , 4F
#include <cstring> 可以嗎
06/15 14:37, 4F

06/15 14:38, , 5F
#include <cstring> using namespace std;
06/15 14:38, 5F

06/15 14:38, , 6F
當然, 在 src file 做, 不要在 header 寫上面的東西
06/15 14:38, 6F

06/15 14:38, , 7F
(using namspace std)
06/15 14:38, 7F

06/15 15:40, , 8F
<string.h> <string> <cstring> 都是不一樣的東西
06/15 15:40, 8F

06/15 15:47, , 9F
一是C的 三是一的C++ namespace版本 二才是C++的string
06/15 15:47, 9F

06/15 23:16, , 10F
06/15 23:16, 10F
文章代碼(AID): #1C5nt9bY (C_and_CPP)
文章代碼(AID): #1C5nt9bY (C_and_CPP)