看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《FiveAaaa ([5A]aaa￾  N  I》之銘言: : 推 FiveAaaa:我檢查過了...都沒有名稱上的錯誤 11/02 15:45 : 推 LPH66:參數型態和回傳形態以及函式大小寫都要一樣... 11/02 15:52 : 推 FiveAaaa:我也檢查了大小寫...但是沒有不同阿ToT怎麼辦 11/02 17:45 : 推 LPH66:那還是貼個code看看好了@@| 11/02 18:39 : 我的code, 有點長= = http://w3.nctu.edu.tw/~u9212037/ : 裡面有1些標頭檔是在 : http://www1.pacific.edu/~wford/fordtopp/cs2book/cs2_index.html 下載的, : 我是用VC++6.0的那個壓縮檔 : http://www1.pacific.edu/~wford/fordtopp/cs2book/ftsoftds_VC++6.zip : 如果各位能播空看看, 小弟將感激不盡! 好多錯...orz ===================分隔線=================== 1.函式宣告不應該在main裡面 要讓它出現在外面 所以把那十幾行函式宣告搬出main 放在enum下面 但是這麼一來會出現15個error 先來看第一個 xxx.cpp(15) : error C2955: 'vector' : use of class template requires template argument list 指向剛搬出去的宣告第一行 這是因為vector是有template的 你忘了附上 ===================分隔線=================== 2.把前面七行宣告中所有的vector加上<T> 前面加上template <class T> 加完再compile 這次又出現link error 仍然是timesort函式的問題 (其實這才是你的問題 但是這問題...嗯...等等你就知道了...) 看看timesort函式的實作 發現有加上template <class T> 於是照加 ===================分隔線=================== 3.加好再compile一次 這次出現16個error 指向呼叫timesort的16行 這裡問題來了 它的錯誤訊息是: xxx.cpp(39) : error C2783: 'void __cdecl timesort(enum sortType,enum testType, int)' : could not deduce template argument for 'T' 它不能決定timesort是用哪一個 T 到底是int/char/long/double/...哪一個都不知道 那要怎麼讓compiler知道我們要用哪一個 T 呢? 我想到的做法是傳一個變數值進去 值本身沒有意義 只是用來指出型態 所以我把函式宣告改成這樣: template <class T> void timesort(sortType stype, testType tst, int vecSize, T typevar); 然後在所有呼叫的地方加傳一個參數0 表示我們要用int ex: timesort(Selection, Rand, nEx, 0); 當然實作的地方也要改 ===================分隔線=================== 4.改好了之後compile, 這次還是15個錯, 不過指向了timesort內部了 第一個是: xxx.cpp(84) : error C2065: 'create' : undeclared identifier 咦? 你不是寫了create函式了嗎? 回頭看宣告: template <class T> vector<T>& Create(testType t, int n); O my god.... 再來看第二個: xxx.cpp(84) : error C2065: 'vecSiZe' : undeclared identifier 等等..vecSiZe不是參數嗎? 看宣告: void timesort(sortType stype, testType tst, int vecSize, T typevar) ...........orz 然後一看, 下面的五組全部都打錯 orz orz orz ===================分隔線=================== 5.費了一番工夫把五個全改回來 再度compile 13個error... 第一個是: xxx.cpp(84) : error C2783: 'class std::vector<T,class std::allocator<T> > &__cdecl Create(enum testType,int)' : could not deduce template argument for 'T' 指向 vector<T>&testv=Create(tst, vecSize); 這一行 和上面3.的原因一樣 無法決定T 所以照3.的方法修改 只是這次直接把typevar傳進去 因為它就是我們要的T 順便把R,A,D三個函式也改好 ===================分隔線=================== 6.改好後再度compile 8個error 第一個說: xxx.cpp(85) : error C2318: no try block associated with this catch handler 仔細看code...同學 你寫了catch忘了try啦.... ===================分隔線=================== 7.把五個try全部補上 再compile (你在case Insertion:的宣告忘了;結束...) 終於剩下2個error了... 2個分別是: xxx.cpp(94) : error C2065: 'testv' : undeclared identifier xxx.cpp(97) : error C2065: 'tshow' : undeclared identifier 嗯? 不是testv已經宣告了嗎? 怎麼還說沒有? 其實是因為你把宣告擺在try{}的大括號裡的關係... 在任何{}大括號中宣告的變數 一旦離開了{} 就會變成未宣告 所以要做的第一件事是把testv的宣告拉出來 第二個錯: tshow是啥東東? 似乎是要呼叫show的樣子? 所以把tshow改成show ===================分隔線=================== 8.再度compile 1個error xxx.cpp(80) : error C2530: 'testv' : references must be initialized 這是在說reference一宣告就要初始化 不過看看你的五個初始化 呼叫的都是Create 參數都一樣 那我們可以把五個初始化統一 在函式開始就宣告好 然後把整個switch塞進一個try裡 最後用一個catch來接錯誤 (要這麼做是為了避免7.的問題) ===================分隔線=================== 9.再度compile 3個error 不過已經出了timesort的範圍了 可喜可賀XD error都是一樣的: xxx.cpp(210) : error C2065: 'createR' : undeclared identifier xxx.cpp(212) : error C2065: 'createA' : undeclared identifier xxx.cpp(214) : error C2065: 'createD' : undeclared identifier 回頭看宣告....同學 你又把C的大小寫打錯了~"~ ===================分隔線=================== 10.改好後再次compile....怎麼又是3個error?! 錯誤訊息告訴你CreateR吃兩個參數..所以是我們在5.時加的typevar忘了傳過去 所以和5.一樣依樣畫葫蘆 把typevar往下傳 ===================分隔線=================== 11.compile...9個error<囧> 第一個告訴你全域函式沒有this.... 同學 這裡不是vector物件 這裡是全域函式啊<囧> 你得宣告一個vector出來 回傳其參考啊... 所以得要像這樣改: template<typename T> vector<T>& CreateR(int n, T typevar) { static vector<T> v; int i; for(i=0;i<n;i++) v.push_back(rnd.random(1000000)); return v; } 宣告加static是防止它出了{}就被摧毀 ===================分隔線=================== 12.再次compile...2個link error xxx.obj : error LNK2001: unresolved external symbol "void __cdecl shellSort( xxx.obj : error LNK2001: unresolved external symbol "void __cdecl exchangeSort( <error message 後略> 看出什麼不對了沒? 你又打錯大小寫了啦<囧> ===================分隔線=================== 13.把實作裡的兩個sort改成Sort 再次build... 5 error 2 warning orz orz orz orz orz orz orz 第一個錯誤指在shellSort的宣告區: vector<T>& t1v,t2v,finalv=v; xxx.cpp(184) : error C2530: 't1v' : references must be initialized 和8.一樣的錯誤訊息.... 這告訴你 reference必須打一開始就必須初始化 這種情況倒不如用個指標去指它 所以把這個函式裡用到的reference全部改成指標 template<typename T> void shellSort(vector<T>& v) { int g=v.size(); int h,i,j,k; vector<T> *t1v,*t2v,*finalv=&v; for(k=1;k<=n/9;k=3*k+1) { //怎麼這裡有個空迴圈? 記得把它填起來啊 } for(h=k;h>0;h--) { t2v=finalv; for(i=0;i<finalv->size();i++) finalv->pop_back(); //嗯?是不是忘了()?我幫你補上了 for(i=0;i<h;i++) { for(j=0;j<t1v->size();j++) t1v->pop_back(); //,這裡也忘了() for(j=i,j<g;j+=h) t1v->push_back((*t2v)[j]); insertionsort(*t1v); join(*finalv,*t1v); } } v=*finalv; } 然後第二個error message: xxx.cpp(185) : error C2065: 'n' : undeclared identifier 這個更有趣XDrz n是長度吧? 那不是已經放在g了? 所以把n改成g ===================分隔線=================== 14.改完再度compile: 3 errors 第一個 xxx.cpp(197) : error C2143: syntax error : missing ';' before ')' 指向一個for 仔細看: 同學 你把第一個;打成,了 @_@ 剩下的兩個: xxx.cpp(199) : error C2065: 'insertionsort' : undeclared identifier xxx.cpp(200) : error C2065: 'join' : undeclared identifier 第二個...你是想呼叫函式庫裡的insertionSort吧 @_@; 第三個...你想把兩個vector接起來? 可是並沒有join這個函式啊... 就算用(*finalv).join(*t1v);也不對 因為vector沒有提供join這個函式 這個最後的問題就要你自己想辦法了... (這我真的幫不到 囧興~) -- 好累..囧 一個錯拉出一大票錯出來.... (而且你果然打錯了不少大小寫....@@|) -- "LPH" is for "Let Program Heal us".... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.240.54
aecho:@@ 你真是好人~~ 給你加加油~~ 11/02 21:01
tititata:雖然我是路人甲....不過還是要說:辛苦了 11/02 21:07
FiveAaaa:我...我好想請你吃王品...淚 11/02 21:45
godfat:囧...有點想說同學是不是太無聊了,來幫我寫作業好了... 11/02 22:00
pcjustin:Orz,真是debug神人,我還沒debug過那麼多錯 11/03 00:30
embedded:台大來的就是不一樣呀.....威呀 11/03 18:59
asontsao:112果然是我們113的強勁對手阿. 11/06 21:22