我一直在尝试为分数类的私有成员分配一个值,但是我被卡在ADD
函数(友谊过程)并且每次都得到以下错误。
错误:' num'未在此范围内声明
#include <iostream>
#include <string.h>
using namespace std;
class fraction
{
long num;
long den;
public:
fraction ();
fraction(long,long);
fraction(const fraction&);
~fraction();
long getnum (void);
long getDen (void);
void print (void);
friend void add (const fraction &, const fraction &);
void sub (fraction, fraction);
void mult (fraction, fraction);
void div (fraction, fraction);
void inc (fraction);
};
fraction::fraction(long l_num,long l_den )
{
num = l_num ;
den = l_den ;
}
fraction::fraction( )
{
num = 0L ;
den = 1L ;
}
fraction::fraction(const fraction& F)
{
num=F.num;
den=F.den;
}
fraction:: ~fraction ( )
{
// cout<<"Destructor"<<endl;
}
long fraction::getDen (void)
{
return den ;
}
long fraction::getnum (void)
{
return num ;
}
void fraction:: print (void)
{
if(den==0)
den=1L;
cout<<num<<"/"<<den <<endl;
}
void add( fraction &f1, fraction &f2)
{
num = (f1.getnum ( ) * f2.getDen ( )) + ( f1.getDen ( ) * f2.getnum ( ) );
den = (f1.getDen ( ) * f2.getDen ( ));
}
void fraction::sub (fraction f1, fraction f2)
{
num = (f1.getnum ( ) * f2.getDen ( )) - ( f1.getDen ( ) * f2.getnum ( ) );
den = (f1.getDen ( ) * f2.getDen ( ));
}
void fraction::mult (fraction f1, fraction f2)
{
num = (f1.getnum ( ) * f2.getnum ( ));
den = (f1.getDen ( ) * f2.getDen ( ));
}
void fraction::div(fraction f1, fraction f2)
{
num = (f1.getnum ( ) * f2.getDen ( ));
den = (f1.getDen ( ) * f2.getnum ( ));
}
void fraction::inc (fraction f1)
{
num = (f1.getnum ( )) + ( f1.getDen ( ) );
den = (f1.getDen ( ) );
}
int main()
{
fraction f1,f2(5L,0L);
fraction f3(f2);
f1.print();
f2.print();
f3.print();
f3 = add(f3 , fraction (-7,8) );
f1 = add(f2,f3);
f1.print ();
f1 = sub(f2 , f3);
f1.print();
f1 = mult(f2, f3);
f1.print();
f1.inc ().inc().print();
f1=div(f2 , f3 );
f1.print();
return 0;
}
答案 0 :(得分:0)
C ++中的成员函数可以访问成员变量,而无需在它们之前显式指定this.
。您可以在代码中利用这一点。但是,朋友的功能不能。在朋友函数中,您需要指定f1.num
或f2.num
(或您要更改的任何分数实例),而不仅仅是num
。
答案 1 :(得分:0)
您可以这样设计[root@ip-172-31-60-222 system]# systemctl daemon-reload
[root@ip-172-31-60-222 system]# systemctl restart celery
[root@ip-172-31-60-222 system]# systemctl status celery.service -l
● celery.service - datasidekick celery service
Loaded: loaded (/etc/systemd/system/celery.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Wed 2017-03-01 04:16:33 UTC; 900ms ago
Process: 22969 ExecStart=/home/datasidekick/datasidekick/newrelic.ini newrelic-admin run-program /home/datasidekick/datasidekick/venv/bin/celery -A datasidekick worker -l info -c 4 (code=exited, status=203/EXEC)
Main PID: 22969 (code=exited, status=203/EXEC)
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: Unit celery.service entered failed state.
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: celery.service failed.
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: celery.service holdoff time over, scheduling restart.
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: start request repeated too quickly for celery.service
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: Failed to start datasidekick celery service.
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: Unit celery.service entered failed state.
Mar 01 04:16:33 ip-172-31-60-222.ec2.internal systemd[1]: celery.service failed.
功能:
add
fraction add( const fraction &f1, const fraction &f2)
{
long num = (f1.getnum ( ) * f2.getDen ( )) + ( f1.getDen ( ) * f2.getnum ( ) );
long den = (f1.getDen ( ) * f2.getDen ( ));
return fraction(num, den);
}
如果以这种方式使用,则不再需要add
函数。
您必须对getter函数进行更多编辑:
friend
请注意,您以这种方式使用long fraction::getDen (void) const
{
return den ;
}
long fraction::getnum (void) const
{
return num ;
}
:
add
因此,从f1 = add(f2,f3);
返回空格对你没有帮助。
话虽如此,您应该在代码中看到其他错误