我一直在試圖將值分配給一小部分類的私有成員,但我被困在ADD
功能(友誼的過程),每次提示以下錯誤:的私有成員變量。在C++值分配給一類
error: 'num' was not declared in this scope
#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;
}
當我所做的所有的變化,我收到標識返回1個錯誤 – mwater07
請重新檢查,你可能會定義一個函數不止一次。 – Rishi