Need help with my code.

Hello! I have a question about how to make this compile without having any errors occur. It is a homework assignment, however, I already had it done. My hard drive went corrupt and I had to completely rewrite. I had someone from school help me with the details of it, and I can't get in with them until later this week and my professor only gave me until tomorrow to finish it. Any help would be much appreciated. currently I have this code.


#include "Rational.h"


using namespace std;

RationalNumber::RationalNumber() {

num = 0;
den = 1;

}


RationalNumber(int q, int r){

num = q;
den = r;

}
RationalNumber(double);

RationalNumber::RationalNumber(RationalNumber &q) {

num = q.num;
den = q.den;

}

~RationalNumber();

int getNumerator();
int getDenominator();

void add(RationalNumber &q) {

num = q.den * num + den * q.num;
den *= q.den;

int d = prvGCD(num, den);

num /= d;
den /= d;
}
void add(RationalNumber &, RationalNumber &r) {

num = r.den * num + den * r.num;
den *= r.den;

int d = prvGCD(num, den);

num /= d;
den /= d;
}
void sub(RationalNumber &) {


num = q.den * num - den * q.num;
den *= q.den;

int d = prvGCD(num, den);

num /= d;
den /= d;


}
void sub(RationalNumber &, RationalNumber &){

num = r.den * num - den * r.num;
den *= r.den;

int d = prvGCD(num, den);

num /= d;
den /= d;

}
void mul(RationalNumber &){


num = q.den * num * den * q.num;
den *= q.den;

int d = prvGCD(num, den);

num /= d;
den /= d;
}
void mul(RationalNumber &, RationalNumber &){

num = r.den * num * den * r.num;
den *= r.den;

int d = prvGCD(num, den);

num /= d;
den /= d;

}
void div(RationalNumber &){


num = q.den * num / den * q.num;
den *= q.den;

int d = prvGCD(num, den);

num /= d;
den /= d;
}
void div(RationalNumber &, RationalNumber &){

num = r.den * num / den * r.num;
den *= r.den;

int d = prvGCD(num, den);

num /= d;
den /= d;

}

bool isEqual(RationalNumber &){

if (num * q.den == den * q.num);

return true;
else
return false;
}
bool isLessThan(RationalNumber &) {

if (num * q.den < den * q.num);

return true;
else
return false;
}

//Cross multiply the two rational numbers
}

int RationalNumber::prvGCD(int a, int b)
int r;

while (b != 0) {
r = a % b;
a = b;
b = r;

}


};


Heres my header file:


#ifndef ____Rational__
#define ____Rational__

#include <iostream>
#include <cmath>

class RationalNumber {
public:
RationalNumber();
RationalNumber(int, int);
RationalNumber(double);
RationalNumber(RationalNumber &);

~RationalNumber();

int getNumerator();
int getDenominator();

void add(RationalNumber &);
void add(RationalNumber &, RationalNumber &);
void sub(RationalNumber &);
void sub(RationalNumber &, RationalNumber &);
void mul(RationalNumber &);
void mul(RationalNumber &, RationalNumber &);
void div(RationalNumber &);
void div(RationalNumber &, RationalNumber &);

bool isEqual(RationalNumber &);
bool isLessThan(RationalNumber &);

private:
int prvGCD(int,int);

int num,den;

};

#endif /* defined(____Rational__) */



Thank you.



(I know that asking someone to DO your homework for you is morally wrong, and I can imagine that there are rules against it on these forums, however since I've already done the majority of the work, and am just asking for specific help, I don't see the harm in it.)
Have you tried compiling it? What errors do you have? Be specific.
This is quite long that's why I didn't include it the first time around.


Bens-MacBook-Pro:Downloads mistaacula$ gcc /Users/mistaacula/Desktop/Rational/Rational.cpp
/Users/mistaacula/Desktop/Rational/Rational.cpp:22: error: expected unqualified-id before ‘int’
/Users/mistaacula/Desktop/Rational/Rational.cpp:22: error: expected `)' before ‘int’
/Users/mistaacula/Desktop/Rational/Rational.cpp:28: error: expected unqualified-id before ‘double’
/Users/mistaacula/Desktop/Rational/Rational.cpp:28: error: expected `)' before ‘double’
/Users/mistaacula/Desktop/Rational/Rational.cpp:37: error: expected constructor, destructor, or type conversion before ‘;’ token
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void add(RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:44: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.h:42: error: ‘int RationalNumber::den’ is private
/Users/mistaacula/Desktop/Rational/Rational.cpp:44: error: within this context
/Users/mistaacula/Desktop/Rational/Rational.cpp:44: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.h:42: error: ‘int RationalNumber::num’ is private
/Users/mistaacula/Desktop/Rational/Rational.cpp:44: error: within this context
/Users/mistaacula/Desktop/Rational/Rational.h:42: error: ‘int RationalNumber::den’ is private
/Users/mistaacula/Desktop/Rational/Rational.cpp:45: error: within this context
/Users/mistaacula/Desktop/Rational/Rational.cpp:47: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void add(RationalNumber&, RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:54: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.h:42: error: ‘int RationalNumber::den’ is private
/Users/mistaacula/Desktop/Rational/Rational.cpp:54: error: within this context
/Users/mistaacula/Desktop/Rational/Rational.cpp:54: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.h:42: error: ‘int RationalNumber::num’ is private
/Users/mistaacula/Desktop/Rational/Rational.cpp:54: error: within this context
/Users/mistaacula/Desktop/Rational/Rational.h:42: error: ‘int RationalNumber::den’ is private
/Users/mistaacula/Desktop/Rational/Rational.cpp:55: error: within this context
/Users/mistaacula/Desktop/Rational/Rational.cpp:57: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void sub(RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:65: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:65: error: ‘q’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:65: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:68: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void sub(RationalNumber&, RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:77: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:77: error: ‘r’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:77: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:80: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void mul(RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:89: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:89: error: ‘q’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:89: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:92: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void mul(RationalNumber&, RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:99: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:99: error: ‘r’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:99: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:102: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void div(RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:111: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:111: error: ‘q’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:111: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:114: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘void div(RationalNumber&, RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:121: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:121: error: ‘r’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:121: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:124: error: ‘prvGCD’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp: In function ‘bool isEqual(RationalNumber&)’:
/Users/mistaacula/Desktop/Rational/Rational.cpp:133: error: ‘num’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:133: error: ‘q’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:133: error: ‘den’ was not declared in this scope
/Users/mistaacula/Desktop/Rational/Rational.cpp:136: error: expected `}' before ‘else’
/Users/mistaacula/Desktop/Rational/Rational.cpp: At global scope:
/Users/mistaacula/Desktop/Rational/Rational.cpp:136: error: expected unqualified-id before ‘else’
/Users/mistaacula/Desktop/Rational/Rational.cpp:138: error: expected declaration before ‘}’ token


sorry for being so lengthy. I'm slowly trying to figure them out one by one but I get stumped sometimes. I've already reduced the size of the list to about half. I'm just lost again.
closed account (zb0S216C)
theebmg wrote:
1
2
3
4
5
6
7
RationalNumber(int q, int r){

num = q;
den = r;

}
RationalNumber(double);

What's going on here? This should be:

1
2
3
4
5
6
7
8
9
RationalNumber::RationalNumber(int q, int r)
    : num(q), den(r)
{
}

RationalNumber::RationalNumber(double param)
    : /* ... */
{
}

Wazzak
Thanks! I had a friend help me along with my professor the first around, and I made the same mistake then. I'll go ahead and edit that. Thanks !

This should be:
1
2
3
4
5
6
7
8
9
RationalNumber::RationalNumber(int q, int r)
    : num(q), den(r)
{
}

RationalNumber::RationalNumber(double param)
    : /* ... */
{
}



so it should used like this if we're define a class on .cpp file? i just know it...
Topic archived. No new replies allowed.