This line: "friend ostream& operator<<(ostream& os, Fraction&& obj)" It works on Visual Studio,But on CodeBlocks it doesn't work. and the error is "expected ',' or '...' before '&&' token|"
#include <iostream>
#include <cstdio>
using namespace std;
int lcf(int n, int m)
{
if (n < m)
{
int tmp = n;
n = m;
m = tmp;
}
if (n % m == 0)
return m;
else
return lcf(m, n % m);
}
class Fraction
{
private:
int fenzi, fenmu;
public:
Fraction(int a, int b) :fenzi(a), fenmu(b) {}
Fraction operator+(Fraction& another)
{
fenzi += another.fenzi;
fenmu += another.fenmu;
return *this;
}
Fraction operator*(Fraction& another)
{
fenzi *= another.fenzi;
fenmu *= another.fenmu;
return *this;
}
operator double()
{
return 1.0* fenzi / fenmu;
}
friend istream& operator>>(istream& is, Fraction& obj)
{
is >> obj.fenzi;
getchar();
is >> obj.fenmu;
return is;
}
friend ostream& operator<<(ostream& os, Fraction&& obj)
{
int t = lcf(obj.fenzi, obj.fenmu);
obj.fenzi /= t;
obj.fenmu /= t;
os << obj.fenzi << "/" << obj.fenmu;
return os;
}
Fraction& operator++()
{
fenzi++;
fenmu++;
return *this;
}
Fraction operator++(int)
{
Fraction tmp(fenzi, fenmu);
fenzi++;
fenmu++;
return tmp;
}
};
int main()
{
Fraction a1(9, 11), a2(1, 2);
cout << double(a2) << endl;
cout << ++a1 << endl;
cout << a2++ << endl;
cout << a1 * a2 << endl;
return 0;
}
if the code is: "friend ostream& operator<<(ostream& os, Fraction& obj)" in the main function,"cout << a1 * a2 << endl" will strangely call the "operator double()" function instead of "friend ostream& operator<<()". So I add an "&"(Fraction&& obj),and successfully work on Visual Studio as what I expected.But CodeBlocks has error.How can I solve it.