由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 帮看看C++分子式化简的程序,总是说exprected primary express
相关主题
再请教一个class输出的问题C++疑问
a simple question for C++ classtwo c++ interview questions! (转载)
请问一个exception题目请教一个作用域的问题
ask a C++ inheritance question私有成员不能用类成员函数修改?
C++里面compare double to float
两个继承问题[合集] C++问题(copy constructor)
为什么我看不懂下面的code,是不是水平还不够?c++环境入门问题
C++ 弱问一个setjmp() and longjmp()
相关话题的讨论汇总
话题: inumerator话题: int话题: cfraction话题: getgcd
进入Programming版参与讨论
1 (共1页)
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 )

1 (共1页)
进入Programming版参与讨论
相关主题
setjmp() and longjmp()C++里面
关于c++的constructor的面试题两个继承问题
问一个简单的C++问题为什么我看不懂下面的code,是不是水平还不够?
一个指向指针的指针的引用?C++ 弱问一个
再请教一个class输出的问题C++疑问
a simple question for C++ classtwo c++ interview questions! (转载)
请问一个exception题目请教一个作用域的问题
ask a C++ inheritance question私有成员不能用类成员函数修改?
相关话题的讨论汇总
话题: inumerator话题: int话题: cfraction话题: getgcd