[問題] A類別的函式內 呼叫 A類別函式 出錯
不好意思我又卡關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
06/01 15:32, 1F
@_@ 痾~~什麼意思
※ 編輯: hunandy14 (120.117.72.133), 06/01/2016 16:36:40
→
06/01 18:12, , 2F
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
06/01 22:55, 3F
→
06/01 22:55, , 4F
06/01 22:55, 4F
→
06/01 23:12, , 5F
06/01 23:12, 5F
阿 乾,不好意思耍蠢了。。。。。。。
天啊 過了~~太感謝你了((灑花
→
06/02 00:01, , 6F
06/02 00:01, 6F
→
06/02 00:01, , 7F
06/02 00:01, 7F
→
06/02 10:54, , 8F
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
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
06/02 14:58, 10F
→
06/02 14:59, , 11F
06/02 14:59, 11F
→
06/02 15:01, , 12F
06/02 15:01, 12F
→
06/02 15:02, , 13F
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
討論串 (同標題文章)
完整討論串 (本文為第 1 之 2 篇):