Error LNK 2019 and LNK 1120

Hey everyone I am currently working on a project for my C++ class and we are supposed to make a Rational class using operators and I was doing fine until I ran into these two errors. I tried looking for the solution on how to solve the LNK 2019 and LNK 1120 errors but I couldn't find a solution that could help mine. Below I have included my Rational header file, Rational .cpp file and Driver .cpp file in that respective order. Thanks in advance for anyone who tries to help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Rational class with function prototypes 
#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
using namespace std;

class Rational
{
public:
	Rational(); //Default Constructor
	Rational(int,int);		//Constructor with paramaters 
	~Rational();			//Destructor 
	void setNumerator(int); //Sets numerator
	void setDenominator(int); //Sets denominator
	void Simplify(int,int);	//Simplifys fraction
	Rational operator+(const Rational &);  //Adds two rational numbers
	Rational operator-(const Rational &);	//Subracts two rational numbers
	Rational operator*(const Rational &);	//Multiplys two rational numbers
	Rational operator/(const Rational &);	//Divides two rational numbers
	void rationalNumber();	//prints fraction in a/b form
	void rationalNumber2(); //prints number in floating point format

private:
	int numerator; //private data member for numerator
	int denominator; //private data member for denominator
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include "Rational.h"
using namespace std;

Rational::Rational(int a,int b)
{
	setNumerator(a);
	if(b>0)
		setDenominator(b);
	else
	{
		cout << "The denominator cannot be 0. It will be intialized as 1." << endl;
		denominator=1;
	}
}
Rational::~Rational()
{

}
void Rational::setNumerator(int a)
{
	numerator=a;
}
void Rational::setDenominator(int b)
{
	denominator=b; 
}
void Rational::Simplify(int numerator,int denominator)
{
	int gcd;
	int smallest = numerator > denominator ? numerator : denominator;
	for(int i=1;i<=smallest;i++)
		{
			if(numerator%i==0&&denominator%i==0)
				gcd=i;
		}
	setNumerator(numerator/=gcd);
	setDenominator(denominator/=gcd);
}
void Rational::rationalNumber()
{
	cout << "Rational Number 1=" << numerator << '/' << denominator << endl;
}
Rational Rational::operator+(const Rational &rhs)
{
	Rational temp;
	temp.numerator=(numerator*rhs.denominator)+(rhs.numerator*denominator);
	temp.denominator=denominator*rhs.denominator;
	Simplify(temp.numerator,temp.denominator);
	return Rational(numerator,denominator);
}
Rational Rational::operator-(const Rational &rhs)
{
	Rational temp;
	temp.numerator=(numerator*rhs.denominator)-(rhs.numerator*denominator);
	temp.denominator=denominator*rhs.denominator;
	Simplify(temp.numerator,temp.denominator);
	return Rational(numerator,denominator);
}
Rational Rational::operator*(const Rational &rhs)
{
	Rational temp;
	temp.numerator=(numerator*rhs.numerator);
	temp.denominator=denominator*rhs.denominator;
	Simplify(temp.numerator,temp.denominator);
	return Rational(numerator,denominator);
}
Rational Rational::operator/(const Rational &rhs)
{
	Rational temp;
	temp.numerator=(numerator*rhs.denominator);
	temp.denominator=denominator*rhs.numerator;
	Simplify(temp.numerator,temp.denominator);
	return Rational(numerator,denominator);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Rational.h"

using namespace std;

int main()
{
	int a,b,x,y;
	cout << "Enter the value for the first numerator and denominator" << endl;
	cin >> a >> b;
	Rational fraction1(a,b);
	cout << "Enter the value for the second numerator and denominator" << endl;
	cin >> x >> y;
	Rational fraction2(x,y);
	system("pause");
	return 0;
}


And these are the error messages I received
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Rational::Rational(void)" (??0Rational@@QAE@XZ) referenced in function "public: class Rational __thiscall Rational::operator+(class Rational const &)" (??HRational@@QAE?AV0@ABV0@@Z) C:\Users\Bryan\Documents\Visual Studio 2010\Projects\Project\Project\Rational.obj
Error 2 error LNK1120: 1 unresolved externals C:\Users\Bryan\Documents\Visual Studio 2010\Projects\Project\Debug\Project.exe 1
Last edited on
You've declared a default constructor for Rational, but you haven't defined it.
Can't believe I overlooked that. Thanks for helping me out I appreciate it.
You're welcome :)
Topic archived. No new replies allowed.