C++类里的友元函数不能访问私有成员(我是用VS2010编的),求解答,这问题困扰我好久了
#include"stdafx.h"#include<iostream>#include"rational.h"usingnamespacestd;intRational...
#include"stdafx.h"
#include<iostream>
#include"rational.h"
using namespace std;
int Rational::gcd(int x, int y)
{
while (y != 0)
{
int rem = x % y;
x = y;
y = rem;
}
return x;
}
void Rational::reduce()
{
int g = gcd(num,den);
if(g > 1)
{
num /= g;
den /= g;
}
}
istream& operator>>(istream& is, Rational& r)
{
char c;
is >> r.num >> c;
if (c == '/')
is >> r.den;
else
{
r.den = 1;
is.putback(c);
}
r.reduce();
return is;
} 展开
#include<iostream>
#include"rational.h"
using namespace std;
int Rational::gcd(int x, int y)
{
while (y != 0)
{
int rem = x % y;
x = y;
y = rem;
}
return x;
}
void Rational::reduce()
{
int g = gcd(num,den);
if(g > 1)
{
num /= g;
den /= g;
}
}
istream& operator>>(istream& is, Rational& r)
{
char c;
is >> r.num >> c;
if (c == '/')
is >> r.den;
else
{
r.den = 1;
is.putback(c);
}
r.reduce();
return is;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询