所以我查了一下,因爲這對於大多數C++學生來說似乎是一個常見的作業問題,但我似乎無法找到能解答我的問題的問題。我覺得我已經正確填寫了代碼,但每次都會收到相同的錯誤。重載運算符Rational錯誤
這裏是我的代碼:
#include <iostream>
using namespace std;
class Rational
{
public:
Rational() {
num = 0;
denom = 1;
};
Rational(int n, int d) {
num = n;
denom = d;
normalize();
}
Rational(int n) {
num = n;
denom = 1;
}
int get_numerator() const {
return num;
}
int get_denominator() const {
return denom;
}
void normalize() {
if ((num > 0 && denom < 0)||(num < 0 && denom < 0)) {
num = -1 * num;
denom = -1 * denom;
}
int gcdcheck = GCD(num,denom);
num = num/gcdcheck;
denom = denom/gcdcheck;
}
int Rational::GCD(int n, int d) {
int temp;
n = abs(n);
d = abs(d);
if (n > d) {
// Do nothing everything is where it should be
}
else {
temp = n;
n = d;
d = temp;
}
int factor = n % d;
while (factor != 0) {
factor = n % d;
d = n;
n = factor;
}
return d;//Return the value to normalize to simplify the fractions to simplist form
}
Rational operator+(Rational b) const {
Rational add;
//Addition of fractions (a*d/b*d + c*b/d*b)
//Numerator = (a*d + c*b)
add.get_numerator = b.get_numerator * denom + b.get_denominator * num;
//Denomenator = (b*d)
add.get_denominator = b.get_denominator * denom;
add.normalize();
return add;
}
Rational operator-(Rational b) const {
Rational sub;
//Same as Addition just a minus sign
//Numerator = (a*d + c*b)
sub.get_numerator = b.get_numerator * denom + b.get_denominator * num;
//Denomenator = (b*d)
sub.get_denominator = b.get_denominator * denom;
sub.normalize();
return sub;
}
Rational operator*(Rational b) const {
//Multiply the numerators and denomenators
Rational multi;
multi.get_numerator = b.get_numerator * num;
multi.get_denominator = b.get_denominator * denom;
multi.normalize();
return multi;
}
Rational operator/(Rational b) const {
//Division of fractions is done by the recipricol of one of the fractions
Rational divi;
divi.get_numerator = b.get_numerator * denom;
divi.get_denominator = b.get_denominator * num;
divi.normalize();
return divi;
}
//To avoid issues with rounding the compare functions will multiply instead to give clean whole numbers
//This will be done by multiplying the denomenators by the opposite numerator
bool operator==(Rational b) const {
return ((b.get_numerator * denom == b.get_denominator * num));
}
bool operator<(Rational b) const {
return ((b.get_numerator * denom > b.get_denominator * num));
}
double toDecimal() const {
double result;
result = static_cast<double> (num)/ static_cast<double> (denom);
return result;
}
private:
int num = 0; // default value is 0
int denom = 1; // default value is 1
};
ostream& operator<<(std::ostream& output, Rational& a) {
if (a.get_denominator == 0) {
output << "Divide by Zero";
}
output << a.get_numerator << '/' << a.get_denominator;
return output;
}
我知道它有很多的代碼,我不希望有人通過它去所有調試我只是想我會後這一切只是在情況下,問題的跨度那麼我認爲問題在哪裏。
我得到同樣的錯誤,每個操作員:
1:錯誤C3867: '理性:: get_denominator':非標準語法;使用'&'創建指向成員的指針
2:'*':錯誤C3867:'Rational :: get_denominator':非標準語法;使用'&'創建指向成員的指針
3:錯誤C3867:'Rational :: get_numerator':非標準語法;使用「&」創建一個指向成員
我已經看過從已經完成的這個問題,並嘗試他們的方法不同的在線網站的代碼,但它似乎並沒有工作。我已經將const和&添加到函數中的參數中,並且我仍然遇到相同的問題。我是否錯誤地調用函數或初始化一個錯誤?
所以,當你的建議我使用set_function你的意思設置輸入等於或者分子或分母。 '有理set_denom(int d){d = denom}'? – Motorscooter
@Motorscooter我已經更新了答案。請注意,在你的評論中你正在做'd = denom';應該做相反的'denom = d'; – knightrider
非常感謝您的澄清,這是現在有道理。只是爲了確保..然後我會使用新的set_denom函數來設置我在每個函數中返回的有理值的分子和分母。 'add.set_num()&add.set_denom'?這樣我沒有返回一個私有變量的引用是否正確? – Motorscooter