Whenever I try to compile my code in Visual C++ 6.0, I get three errors: driver.obj : error LNK2001: unresolved external symbol "public: __thiscall Rational::Rational(int,int)" (??0Rational@@QAE@HH@Z)
Rational.obj : error LNK2001: unresolved external symbol "public: __thiscall Rational::Rational(int,int)" (??0Rational@@QAE@HH@Z)
Debug/driver.exe : fatal error LNK1120: 1 unresolved externals
My code is:
// driver.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include "Rational.h"
int main()
{
int a1;
int a2;
int b1;
int b2;
cout<<"Input the numerator of the first number.";
cin>>a1;
cout<<"Input the denominator of the second number.";
cin>>a2;
cout<<"Input the numerator of the second number.";
cin>>b1;
cout<<"Input the denominator of the second number.";
cin>>b2;
Rational c( a1, a2 ), d( b1, b2 ), x; // creates three rational objects
c.printRational(); // prints rational object c
cout << " + ";
d.printRational(); // prints rational object d
x = c.addition( d ); // adds object c and d; sets the value to x
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n'; //New line
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << "\n\n"; //Skip a line
c.printRational(); // prints rational object c
cout << " - ";
d.printRational(); // prints rational object d
x = c.subtraction( d ); // subtracts object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << "\n\n";
c.printRational(); // prints rational object c
cout << " x ";
d.printRational(); // prints rational object d
x = c.multiplication( d ); // multiplies object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << "\n\n";
c.printRational(); // prints rational object c
cout << " / ";
d.printRational(); // prints rational object d
x = c.division( d ); // divides object c and d
cout << " = ";
x.printRational(); // prints rational object x
cout << '\n';
x.printRational(); // prints rational object x
cout << " = ";
x.printRationalAsDouble(); // prints rational object x as double
cout << endl;
return 0;
} // end main
// Rational.cpp: implementation of the Rational class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Rational.h"
#include <iostream>
using std::cout;
using std::cin;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Rational Rational::addition(const Rational &m)
{
Rational t;
int lcd;
int mult1;
int mult2;
lcd=Rational::LCM(denominator, m.denominator);
mult1=denominator/lcd;
mult2=m.denominator/lcd;
t.numerator=(mult1 * denominator) + (mult2 * m.denominator);
t.denominator=lcd;
return t;
}
Rational Rational::subtraction(const Rational &m)
{
Rational t;
int lcd;
int mult1;
int mult2;
lcd=Rational::LCM(denominator, m.denominator);
mult1=denominator/lcd;
mult2=m.denominator/lcd;
t.numerator=(mult1 * denominator) - (mult2 * m.denominator);
t.denominator=lcd;
return t;
}
Rational Rational::multiplication(const Rational &m)
{
Rational t;
t.numerator = numerator * m.numerator;
t.denominator = denominator * m.denominator;
return t;
}
Rational Rational::division(const Rational &m)
{
Rational t;
t.numerator = numerator * m.denominator;
t.denominator = denominator * m.numerator;
return t;
}
void Rational::printRational()
{
cout << numerator<<"/"<<denominator;
}
void Rational::printRationalAsDouble()
{
cout<<numerator/1.0/denominator;
}
int Rational::LCM(int x,int y)
{
int prod;
if(y%x==0)
return y;
else
{
prod=x*y;
while(x!=y) // get the GCD of 2 given integers
{
if(x>y)
x=x-y;
else
y=y-x; //x now is the GCD
}
return LCM(y,prod/x); //recurse, changing x to y and vice versa
} //LCM = (x*y)/(GCD)
}
// Rational.h: interface for the Rational class.
//
//////////////////////////////////////////////////////////////////////
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational &); // function addition
Rational subtraction( const Rational & ); // function subtraction
Rational multiplication( const Rational & ); // function multi.
Rational division( const Rational & ); // function division
void printRational (); // print rational format
void printRationalAsDouble(); // print rational as double format
int LCM(int x, int y);
private:
int numerator; // integer numerator
int denominator; // integer denominator
void reduction(); // utility function
}; // end class Rational
#endif
Sorry for the long post, I just haven't been able to figure this out :'(