Мои классы фракций перегруженных операторов не будут компилироваться, заявив, что они должны быть унарным или двоичным оператором. (C++) После поиска, я нашел совет, который заставил их друзей удалить неявный первый термин, но мои операторы были друзьями все это время и все еще возвращают «ошибка: перегруженный оператор» - должен быть унарным или двоичным оператор (имеет 3 параметра) ", когда я иду на компиляцию.(C++) Мои перегруженные функции оператора все возвращают ошибку, что они должны быть унарными или двоичными, несмотря на то, что они друзья
Здесь они находятся в моем файле заголовка
friend std::ostream& operator<<(std::ostream& os, const Fraction& frac); //printing
friend std::istream& operator>>(std::istream& is, Fraction& frac); // reading
friend const Fraction operator+(const Fraction& x, const Fraction& y); // adding
friend const Fraction operator-(const Fraction& x, const Fraction& y); // subtract
и здесь определения для каждого из этих
std::ostream& Fraction::operator<<(std::ostream& os, const Fraction& frac) //printing
{
if(num % den == 0)
cout << num/den << endl;
else
cout << num << "/" << den << endl;
}
std::istream& Fraction::operator>>(std::istream& is, Fraction& frac) // reading
{
int pc; //peek character
is >> skipws >> num; // read numerator, skipping whitespace
pc = is.peek(); // check next character
if(is && isspace(pc)) //if whitespace after the numerator
{
while(is && isspace(pc))
{
is.get(); // eat space
pc = is.peek(); // move through the stream
}
}
else if(is && pc == '/')
{
is.get(); // eat the '/'
is >> skipws >> den;
}
if(den == 0)
{
throw invalid_argument("denominator is zero");
}
if(den < 0)
{
num = num * -1;
den = den * -1;
}
}
const Fraction Fraction::operator+(const Fraction& x, const Fraction& y) // adding
{
Fraction temp1;
Fraction temp2;
if(x.getden() == y.getden())
{
temp1.set(x.getnum() + y.getnum(), x.getden());
}
else
{
temp1.set(x.getnum() * y.getden(), x.getden() * y.getden());
temp2.set(y.getnum() * x.getden(), y.getden() * x.getden());
temp1.set(temp1.getnum() + temp2.getnum(), temp1.getden());
}
reduceFrac(temp1);
return temp1;
}
const Fraction Fraction::operator-(const Fraction& x, const Fraction& y) // subtracting
{
Fraction temp1;
Fraction temp2;
if(x.getden() == y.getden())
{
temp1.set(x.getnum() - y.getnum(), x.getden());
}
else
{
temp1.set(x.getnum() * y.getden(), x.getden() * y.getden());
temp2.set(y.getnum() * x.getden(), y.getden() * x.getden());
temp1.set(temp1.getnum() - temp2.getnum(), temp1.getden());
}
reduceFrac(temp1);
return temp1;
}
Почему все эти члены «Фракции»? – user2357112
не являются функциями друзей, а не членами класса? – ellihts
Участники класса не нуждаются в дружбе, чтобы получить доступ к частным вещам. – chris