为什么我的友元函数不能访问私有数据成员?

//Rational.h#ifndefRATIONAL_H#defineRATIONAL_HclassRational{public:Rational(inta=0,in... //Rational.h
#ifndef RATIONAL_H
#define RATIONAL_H

class Rational {
public:
Rational(int a=0, int b=1);
Rational operator +(const Rational &a) const;
Rational operator -(const Rational &a) const;
Rational operator *(const Rational &a) const;
Rational operator /(const Rational &a) const;
bool operator ==(const Rational &a) const;
friend ostream & operator << (ostream &out,const Rational &a);
private:
int numerator;
int denominator;
void optimization(); //使有理数约去公因子
};
#endif
//Rational.cpp
#include <iostream>
using namespace std;
#include "Rational.h"

Rational::Rational(int a, int b){
numerator=a;
denominator=b;
optimization();
}

Rational Rational::operator +(const Rational &a) const{
Rational b;
b.numerator=numerator*a.denominator + denominator*a.numerator;
b.denominator=denominator * a.denominator;
b.optimization();
return b;
}

Rational Rational::operator -(const Rational &a) const{
Rational b;
b.numerator=numerator*a.denominator - denominator*a.numerator;
b.denominator=denominator * a.denominator;
b.optimization();
return b;
}

Rational Rational::operator *(const Rational &a) const{
Rational b;
b.numerator=numerator * a.numerator;
b.denominator=denominator * a.denominator;
b.optimization();
return b;
}

Rational Rational::operator /(const Rational &a) const{
Rational b;
b.numerator=numerator * a.denominator;
b.denominator=denominator * a.numerator;
b.optimization();
return b;
}

bool Rational::operator ==(const Rational &a) const{
if(a.numerator==numerator && a.denominator==denominator)
return true;
else return false;
}

ostream & operator << (ostream &out,const Rational &a){
out<<a.numerator<<"/"<<a.denominator<<endl;
return out;
}

int gcd(int m,int n){
if (m < n){
int tmp = m;
m = n;
n = tmp;
}
if (n == 0) return m;
while (n > 0){
int tmp = m % n;
m = n;
n = tmp;
}
return m;
}

void Rational::optimization(){
int a=gcd(numerator,denominator);
numerator=numerator/a;
denominator=denominator/a;
}
//main.cpp
#include <iostream>
using namespace std;
#include "Rational.h"

void main(){
Rational r1(2,3),r2(4,6),r3(2,1),r4;
cout<<"r1="<<r1;
cout<<"r2="<<r1;
cout<<"r3="<<r1;
r4=r1+r3;
cout<<"r1+r3="<<r4;
cout<<"r3-r1="<<r3-r1;
cout<<"r1*r3="<<r1*r3;
cout<<"r1/r3="<<r1/r3;

}
展开
 我来答
125516639
2011-12-15 · TA获得超过452个赞
知道小有建树答主
回答量:184
采纳率:50%
帮助的人:131万
展开全部
LZ做的是一个有理数运算的程序吧
友元函数不能访问私有数据成员指的是编译报错还是输出问题?
就我看到的说一下
cout<<"r1="<<r1;
cout<<"r2="<<r1;
cout<<"r3="<<r1;
的部分输出的东西错了,改为一下会更好
cout<<"r1="<<r1<<endl;
cout<<"r2="<<r2<<endl;
cout<<"r3="<<r3<<endl;
追问
编译报错
error C2248: 'numerator' : cannot access private member declared in class 'Rational'
error C2248: 'denominator' : cannot access private member declared in class 'Rational'
追答
编译器不一样...我这边编译成功... 要不你先去掉所有的const试试,或者写成内联形式看能否编译
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式