这在Visual Studio 2015中运行良好,但在使用MinGw时运行不佳。为什么?如何解决?
错误信息:
不匹配'运营商<<' (操作数类型是' std :: ostream {aka std :: basic_ostream}' Fraction')werjiorqq.cpp / werjiorqq / src第106行C / C ++问题
不匹配'运营商<<' (操作数类型是' std :: ostream {aka std :: basic_ostream}' Fraction')werjiorqq.cpp / werjiorqq / src第107行C / C ++问题
不匹配'运营商<<' (操作数类型是' std :: ostream {aka std :: basic_ostream}' Fraction')werjiorqq.cpp / werjiorqq / src第108行C / C ++问题
不匹配'运营商<<' (操作数类型是' std :: ostream {aka std :: basic_ostream}和' Fraction')werjiorqq.cpp / werjiorqq / src第109行C / C ++问题
没有匹配函数来调用' Fraction :: Fraction(Fraction)' werjiorqq.cpp / werjiorqq / src第71行C / C ++问题
没有匹配函数来调用' Fraction :: Fraction(Fraction)' werjiorqq.cpp / werjiorqq / src第88行C / C ++问题
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int m, int n) {
if (m%n == 0)
return n;
else
return gcd(n, m%n);
}
int preGcd(int m, int n) {
int absm = abs(m);
int absn = abs(n);
int g = gcd(absm, absn);
return g;
}
class Fraction {
int m, n;
public:
Fraction() {
}
Fraction(Fraction& f) {
m = f.getM();
n = f.getN();
}
Fraction(int m, int n) {
this->m = m;
this->n = n;
}
friend ostream& operator<< (ostream& out,Fraction& f) {
cout << f.m << '/' << f.n << endl;
return out;
}
int getM() {
return m;
}
int getN() {
return n;
}
void reduction() {
int g;
if (n > m)
g = preGcd(n, m);
else
g = preGcd(m, n);
m /= g;
n /= g;
}
friend Fraction operator+ (Fraction f1,Fraction f2) {
int nn = f1.n*f2.n;
int nm = f1.m*f2.n + f2.m*f1.n;
Fraction *p = new Fraction(nm, nn);
p->reduction();
return *p;
}
friend Fraction operator- (Fraction f1, Fraction f2) {
f2.m = (-f2.m);
return operator+ (f1, f2);
}
friend Fraction operator* (Fraction f1, Fraction f2) {
/*cout << f1;
cout << f2;*/
int nn = f1.n*f2.n;
int nm = f1.m*f2.m;
Fraction *p = new Fraction(nm, nn);
p->reduction();
return *p;
}
friend Fraction operator/ (Fraction f1, Fraction f2) {
int temp = f2.m;
f2.m = f2.n;
f2.n = temp;
return operator*(f1, f2);
}
void revenue() {
cout << n << '/' << m << endl;
}
};
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
Fraction f1(a, b);
Fraction f2(c, d);
cout << f1 + f2;
cout << f1 - f2;
cout << f1*f2;
cout << f1 / f2;
f1.revenue();
}
答案 0 :(得分:1)
您需要尽可能申请const
。例如:
Fraction(const Fraction &);
friend ostream& operator<< (ostream& out, const Fraction& f);
int getM() const;
int getN() const;
等