[問題] 同樣的程式碼在不同編譯器的問題

看板C_and_CPP作者 (蜻蜓)時間15年前 (2010/06/19 16:14), 編輯推噓4(4013)
留言17則, 5人參與, 最新討論串1/1
這是一個把文字16進位轉換成二進位的小程式 1 #include <cstdlib> 2 #include <iostream> 3 #include <fstream> 4 5 using namespace std; 6 7 int main(int Argc, char *Argv[]) 8 { 9 string FileNameIn(Argv[1]); 10 string Temp; 11 unsigned int Ch; 12 13 ifstream FileIn(FileNameIn.c_str()); 14 ofstream FileOut((FileNameIn + ".out").c_str(), ios::binary); 15 16 cout << FileIn << endl; 17 while(FileIn >> Temp) 18 { 19 sscanf(Temp.c_str(), "%02X", &Ch); 20 FileOut << (char)Ch; 21 } 22 cout << FileIn << endl; 23 24 FileOut.close(); 25 FileIn.close(); 26 27 system("PAUSE"); 28 return EXIT_SUCCESS; 29 } 我有一些問題 1. 我用 Dev-C++ 4.9.9.2 for mingw32 3.4.5 來進行編譯是成功的且正確執行。 但是我用 Microsoft Visual C++ 6.0 結果出現以下訊息: Compiling... main.cpp C:\hex2bin\main.cpp(14) : error C2784: 'class std::reverse_iterator<_RI, _Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse _iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class std::basic_string,class std::allocator >' C:\hex2bin\main.cpp(14) : error C2676: binary '+' : 'class std::basic_string,class std::allocator >' does not define this operator or a conversion to a type acceptable to the predefined operator C:\hex2bin\main.cpp(14) : error C2228: left of '.c_str' must have class/struct/union type C:\hex2bin\main.cpp(17) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string,class std::allocator >' (or there is no acceptable conversion) C:\hex2bin\main.cpp(17) : fatal error C1903: unable to recover from previous error(s); stopping compilation Error executing cl.exe. 這表示我不能這樣寫嗎?那為什麼 Dev-C++ 卻可以呢? 2. 第 16 行和第 22 行分別顯示 FileIn 物件執行前和執行後的指標。 結果是: 0x22fe84 0 如果第二個顯示是 0 那我第 25 行程式還有執行的意義在嗎? 謝謝大家 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 203.68.161.161

06/19 16:20, , 1F
你是不是少 #include<string> 這行...?
06/19 16:20, 1F

06/19 16:37, , 2F
沒錯 確實是少加這一條 編譯成功 真是謝謝了
06/19 16:37, 2F

06/19 16:39, , 3F
可是為什麼 Dev-C++ 不會警告呢,難道它很貼心的自動加入?
06/19 16:39, 3F

06/19 16:40, , 4F
Dev-C++很多黑箱是真的...
06/19 16:40, 4F

06/19 16:41, , 5F
再來是第二個問題,你做cout << FileIn會使得FileIn轉型
06/19 16:41, 5F

06/19 16:42, , 6F
成void*型別,而它的轉型結果是failbit或badbit為1的話就
06/19 16:42, 6F

06/19 16:42, , 7F
轉成NULL,剩下的是轉成隨便的指標。這跟你的FileIn有沒
06/19 16:42, 7F

06/19 16:43, , 8F
有close()無關,所以還是要記得close()
06/19 16:43, 8F

06/19 16:55, , 9F
原來是這樣 我以為已經轉為空指標自己已經銷毀了 謝謝
06/19 16:55, 9F

06/19 17:12, , 10F
<string> 的問題主要在函式庫實作的差異,有的可能會在
06/19 17:12, 10F

06/19 17:12, , 11F
iostream / fstream 裡面偷偷 include 到,所以就能用了。
06/19 17:12, 11F

06/19 17:13, , 12F
但是不應該依賴這種特性,Effective STL 也有寫到。
06/19 17:13, 12F

06/19 17:55, , 13F
我剛剛追了 include 確實 mingw32 在所有的 stream 當中
06/19 17:55, 13F

06/19 17:56, , 14F
都會加入 #include <bits/locale_classes.h> 這個
06/19 17:56, 14F

06/19 17:58, , 15F
而它有 include string 檔頭 所以才編譯都沒問題
06/19 17:58, 15F

06/19 22:48, , 16F
顏色怎麼變的? 是手動一個一個打控碼嗎!!?
06/19 22:48, 16F

06/19 23:41, , 17F
置底"[公告] 張貼程式碼"這篇有連結可以轉
06/19 23:41, 17F
文章代碼(AID): #1C77naSX (C_and_CPP)