I have two .cpp files "rational.cpp" and "main.cpp" and one "rational.h" file. In "rational.cpp" I have a declared a Rational class, and included "rational.h" at the top of both files; however when I try to create the object Rational rat;
. I get the error 'identifier "Rational" is undefined'. I am very confused.
Here is "main.cpp"
#include "rational.h"
int main() {
int select = 1, n, d;
while (select != 0) {
cout << "Enter numerator";
cin >> n;
cout << "\nEnter denominator";
cin >> d;
try {
if (d == 0) {
throw 0;
}
}
catch (int e) {
cout << "/nerror: cannot divide by 0" << endl;
cout << "enter a new denominator: ";
cin >> d;
}
Rational rat;
cout << "1. Add a rational\n2. Subtract a rational\n3. Multiply by a rational\n4. Divide by a rational\n0. Exit";
cout << "enter selector" << endl;
cin >> select;
switch (select) {
case 1: rat.add(n, d); break;
case 2: rat.sub(n, d); break;
case 3: rat.mul(n, d); break;
case 4: rat.div(n, d); break;
case 0: break;
default:
try {
throw select;
}
catch (int e) {
cout << e << " is not a valid option" << endl;
}
catch (...) {
cout << "error: invalid option" << endl;
}
break;
}
}
return 0;
}
"rational.cpp"
#include "rational.h"
class Rational{
public:
void reduce(int n, int d);
//define math functions
void add(int n, int d);
void sub(int n, int d);
void div(int n, int d);
void mul(int n, int d);
};
void Rational::reduce(int n, int d) {
}
void Rational::add(int n, int d) {
}
void Rational::sub(int n, int d) {
}
void Rational::div(int n, int d) {
}
void Rational::mul(int n, int d) {
}
"rational.h"
#pragma once
#ifndef RATIONAL
#define RATIONAL
#include <iostream>
#include <math.h>
#include <exception>
using namespace std;
#endif
and my Makefile
rational.exe: main.o rational.o
g++ -o rational.exe main.o rational.o
main.o: main.cpp rational.h
g++ -c main.cpp
rational.o: rational.cpp rational.h
g++ -c rational.cpp
I am using cygwin to compile.