不好意思我又卡关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)也要改