[問題] A類別的函式內 呼叫 A類別函式 出錯

看板C_and_CPP作者 (Charlott.HonG)時間8年前 (2016/06/01 15:25), 8年前編輯推噓0(0015)
留言15則, 5人參與, 最新討論串1/2 (看更多)
不好意思我又卡關QQ [程式越寫越長了我把她分開了](https://goo.gl/u8lMBP) 我想問的是 Hw10_fun.cpp 的 72~75 行 底下會詳細說明,盡可能不讓你們花太多時間看程式碼 使用GGC編譯錯誤是 In file included from Hw10_main.cpp:8:0: Hw10_fun.cpp: In instantiation of 'frac<T1> frac<T1>::operator+(const frac<T1>&) [with T1 = int]': Hw10_main.cpp:19:11: required from here Hw10_fun.cpp:74:4: error: passing 'const frac<int>' as 'this' argument discards qualifiers [-fpermissive] t2=p.impro(); ^ Hw10_fun.cpp:115:10: note: in call to 'frac<T1> frac<T1>::impro() [with T1 = int]' frac<T1> frac<T1>::impro(){ ^ frac 類別有三個變數,比如說 frac<int> a(1,2,3); 意思是分數 (1又3分之2) impro()是轉換函式 把[帶分數]轉成[真分數] 在main()裡面測試成功是正確的結果 類別內有自定義的運算子,用來處理那三個變數 我想在重載運算子內將類別轉成假分數型態在運算 這樣比較好處理,這個函式也直接公開自由使用 // 運算子重載 template <typename T1> frac<T1> frac<T1>::operator+(const frac &p){ frac<T1> temp; frac<T1> t1,t2; // t1=this->impro(); // t1.pri(); // t2=p.impro(); // t2.pri(); temp.num = this->num * p.den+ this->den * p.num; temp.den = this->den * p.den; // temp.mix = this->mix; temp.flag = this->flag; return temp; } template <typename T1> // 假分數 frac<T1> frac<T1>::impro(){ frac<T1> temp; temp.mix = 0; temp.num = this->mix*this->den + this->num; temp.den = this->den; temp.flag = this->flag; return temp; } 想請問了怎麼會出現這樣的錯誤,哪裡錯了QQ this使用方式是->而不是點 不知道是不是這樣用 我這樣打可以編譯,可是出來的數值沒變 **解答:把呼叫的函數加上 const 修飾 記得定義(.h)也要改 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 120.117.72.133 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1464765942.A.D9C.html

06/01 15:32, , 1F
74行的p是const ref,impro不是const func
06/01 15:32, 1F
@_@ 痾~~什麼意思 ※ 編輯: hunandy14 (120.117.72.133), 06/01/2016 16:36:40

06/01 18:12, , 2F
frac<T1> frac<T1>::impro() const {...}
06/01 18:12, 2F
也不行耶QQ In file included from Hw10_main.cpp:8:0: Hw10_fun.cpp:115:10: error: prototype for 'frac<T1> frac<T1>::impro() const' does not match any in class 'frac<T1>' frac<T1> frac<T1>::impro() const{ ^ In file included from Hw10_fun.cpp:8:0, from Hw10_main.cpp:8: Hw10_fun.h:27:10: error: candidate is: frac<T1> frac<T1>::impro() frac impro(); ※ 編輯: hunandy14 (120.117.72.133), 06/01/2016 21:58:30

06/01 22:55, , 3F
等下.. 你是把這個 template class , 拆成兩個檔案嗎 ?
06/01 22:55, 3F

06/01 22:55, , 4F
.h 用宣告,.cpp 用實作,是這樣嗎?
06/01 22:55, 4F

06/01 23:12, , 5F
.h檔也要加const
06/01 23:12, 5F
阿 乾,不好意思耍蠢了。。。。。。。 天啊 過了~~太感謝你了((灑花

06/02 00:01, , 6F
耶... 我的疑問其實是 , 現在的 compiler 可以支援
06/02 00:01, 6F

06/02 00:01, , 7F
template 可以拆檔實現了?
06/02 00:01, 7F
可以哦,這個我在上一篇已經雷過一次了QQ 可以參考 https://goo.gl/Fpx7lC

06/02 10:54, , 8F
好吧 可能我的 vs 太舊了
06/02 10:54, 8F
最後還有一個小小的問題,不知道有沒有人發現我是 #include .cpp 而不是 include .h 想請問這樣有什麼影響嗎? (我是用 sublimetext 一次只能編一個.cpp 不能夠 gcc main.cpp fun.cpp) ※ 編輯: hunandy14 (114.35.139.127), 06/02/2016 11:02:43

06/02 11:04, , 9F
定義用 const 時宣告也要用 const
06/02 11:04, 9F
哦哦~~~ 那請問 樣板類別 與 類別運算子重載 的函式導入參數,都要加入const定義嗎? 參考自這裡,我看他都有加 [重載](http://goo.gl/zlIvjy) [樣板](http://goo.gl/i94WxU) ※ 編輯: hunandy14 (114.35.139.127), 06/02/2016 13:13:06 ※ 編輯: hunandy14 (114.35.139.127), 06/02/2016 14:35:46

06/02 14:58, , 10F
template一直都可以拆檔 我從vs 2010開始用就是這樣了
06/02 14:58, 10F

06/02 14:59, , 11F
不會怎麼樣,但是為何不把Hw10_fun.cpp與Hw10_fun.h合併
06/02 14:59, 11F

06/02 15:01, , 12F
就算你真的要分開,那Hw10_main.cpp應該要先看到class
06/02 15:01, 12F

06/02 15:02, , 13F
frac的declaration,而不是先看到frac的definition
06/02 15:02, 13F

06/02 15:06, , 14F
你這樣寫,很奇怪而已(然後很容易讓別人誤會)
06/02 15:06, 14F
declaration 跟 definition 是什麼意思QQ 對應 fun.cpp 與 fun.h 的意思嗎 通常都怎麼寫,不是會拆出.h檔 我是參考這篇 http://goo.gl/MQIqOO ※ 編輯: hunandy14 (114.35.139.127), 06/02/2016 15:15:25

06/02 15:19, , 15F
我說錯了,我另外回你一篇文章比較快
06/02 15:19, 15F
我好像了解了些什麼 我剛剛試著把那篇實作分開的加入樣板 沒想到這樣加上去就不能編譯了 看了一下,你那篇好像有答案 花點時間吸收一下,再回在你那篇 ※ 編輯: hunandy14 (114.35.139.127), 06/02/2016 16:18:13 ※ 編輯: hunandy14 (114.35.139.127), 06/07/2016 19:10:11
文章代碼(AID): #1NJetssS (C_and_CPP)
文章代碼(AID): #1NJetssS (C_and_CPP)