a****m 发帖数: 693 | 1 #include
using namespace std;
class CFraction
{
public: //public method to get GCD
int iNumerator;
int iDenominator;
double dValue;
//constructor...
CFraction( int iNumeratorParam, int iDenominatorParam )
{
iNumerator = iNumeratorParam;
iDenominator = iDenominatorParam;
dValue = iNumerator/ iDenominator;
}
//getGCD
int getGCD( int iNumerator, int iDenominator )
{
int remainder = iDenominator % iNumerator;
if ( remainder != 0 )
return getGCD( remainder,iNumerator );
}
//Reduce (what you wanted in your question):
CFraction reduce()
{
int iGCD = getGCD(int iNumerator, int iDenominator);
//CFraction retVal( iNumerator/iGCD, iDenominator/iGCD );
iNumerator=iNumerator/iGCD;
iDenominator=iDenominator/iGCD;
}
//Display fraction value
void display()
{
cout << iNumerator << "/" << iDenominator;
}
}
int main()
{
CFraction fracOriginal( 25, 50 );
CFraction fracReduced = fracOriginal.reduce();
cout << "Reduced fraction is:" << endl;
fracReduced.display();
system("pause");
return 0;
} |
N***m 发帖数: 4460 | 2 你这class 最后没有分号?
【在 a****m 的大作中提到】 : #include : using namespace std; : class CFraction : { : public: //public method to get GCD : int iNumerator; : int iDenominator; : double dValue; : //constructor... : CFraction( int iNumeratorParam, int iDenominatorParam )
|
s***a 发帖数: 299 | 3 //getGCD
int getGCD( int iNumerator, int iDenominator )
{
int remainder = iDenominator % iNumerator;
if ( remainder != 0 )
return getGCD( remainder,iNumerator );
// I think you must add else here since you must return an int here
}
【在 a****m 的大作中提到】 : #include : using namespace std; : class CFraction : { : public: //public method to get GCD : int iNumerator; : int iDenominator; : double dValue; : //constructor... : CFraction( int iNumeratorParam, int iDenominatorParam )
|
a****m 发帖数: 693 | 4 #include
#include "GCD.hpp"
using namespace std;
class CFraction
{
public: //private method to get GCD
int iNumerator;
int iDenominator;
//constructor...
CFraction( int iNumeratorParam, int iDenominatorParam )
{
iNumerator = iNumeratorParam;
iDenominator = iDenominatorParam;
}
//getGCD
int getGCD( int iNumerator, int iDenominator )
{
int remainder = iDenominator % iNumerator;
if ( remainder != 0 )
return getGCD( remainder,iNumerator );
else return 0;
}
CFraction reduce()
{
int iGCD = getGCD(int iNumerator, int iDenominator);
// 总是在这一行出问题: expected primary-expression before "int"
iNumerator=iNumerator/iGCD;
iDenominator=iDenominator/iGCD;
}
};
int main()
{
CFraction fracOriginal( 25, 50 );
CFraction fracReduced = fracOriginal.reduce();
cout << "Reduced fraction is:" << endl;
system("pause");
return 0;
} |
t****t 发帖数: 6806 | 5 不是马上就有人问你class最后为什么没有分号吗?
【在 a****m 的大作中提到】 : #include : #include "GCD.hpp" : using namespace std; : class CFraction : { : public: //private method to get GCD : int iNumerator; : int iDenominator; : //constructor... : CFraction( int iNumeratorParam, int iDenominatorParam )
|
a****m 发帖数: 693 | 6
好像加了还是出现这个问题?
谢谢
【在 t****t 的大作中提到】 : 不是马上就有人问你class最后为什么没有分号吗?
|
a****m 发帖数: 693 | 7 CFraction reduce()
{
int iGCD = getGCD(int iNumerator, int iDenominator);
// 总是在这一行出问题: expected primary-expression before "int"
//It seems that it does not need redefine the type, getGCD(inum, idenom)
iNumerator=iNumerator/iGCD;
iDenominator=iDenominator/iGCD;
}
thanks all you guys! |
N***m 发帖数: 4460 | 8 你不能在函数调用里面加int.
【在 a****m 的大作中提到】 : CFraction reduce() : { : int iGCD = getGCD(int iNumerator, int iDenominator); : // 总是在这一行出问题: expected primary-expression before "int" : //It seems that it does not need redefine the type, getGCD(inum, idenom) : iNumerator=iNumerator/iGCD; : iDenominator=iDenominator/iGCD; : } : thanks all you guys!
|
t****t 发帖数: 6806 | 9 int iGCD = getGCD(int iNumerator, int iDenominator);
哦, 这一行啊, 你看你写的是什么呀? copy/paste也不能这样啊.
【在 a****m 的大作中提到】 : CFraction reduce() : { : int iGCD = getGCD(int iNumerator, int iDenominator); : // 总是在这一行出问题: expected primary-expression before "int" : //It seems that it does not need redefine the type, getGCD(inum, idenom) : iNumerator=iNumerator/iGCD; : iDenominator=iDenominator/iGCD; : } : thanks all you guys!
|
c**b 发帖数: 2999 | 10 1.這個函数有返回值吗? 没看见里面有return;
2.int iGCD = getGCD(iNumerator,iDenominator);
【在 a****m 的大作中提到】 : CFraction reduce() : { : int iGCD = getGCD(int iNumerator, int iDenominator); : // 总是在这一行出问题: expected primary-expression before "int" : //It seems that it does not need redefine the type, getGCD(inum, idenom) : iNumerator=iNumerator/iGCD; : iDenominator=iDenominator/iGCD; : } : thanks all you guys!
|
D***r 发帖数: 7511 | 11 用惯了脚本语言,再看C++简直觉得是折磨
【在 a****m 的大作中提到】 : #include : #include "GCD.hpp" : using namespace std; : class CFraction : { : public: //private method to get GCD : int iNumerator; : int iDenominator; : //constructor... : CFraction( int iNumeratorParam, int iDenominatorParam )
|