I have code here that I am trying to understand operator overloading, I have hit a road block with an error I don't understand fully. The error has 3 LNK2019 errors on it, reading unresolved external symbol. I want to know what the error is and how I can fix it. Thanks for your help in advance.
This is my implementation code.
ComplexNumber::ComplexNumber()
{
double complexNumberValue = 0.00;
double realNumberValue = 0.00;
}
ComplexNumber::ComplexNumber(double real, double imaginary)
{
realNumberValue = real;
complexNumberValue = imaginary;
}
ComplexNumber ComplexNumber::operator*(const ComplexNumber& rho) const
{
double newValue = realNumberValue * rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator+(const ComplexNumber& rho) const
{
double newValue = realNumberValue + rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator-(const ComplexNumber& rho) const
{
double newValue = realNumberValue - rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator/(const ComplexNumber& rho) const
{
double newValue = realNumberValue / rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator=(const ComplexNumber& rho)
{
realNumberValue = rho.realNumberValue;
return *this;
}
bool operator==(const ComplexNumber& leftOp, const ComplexNumber& rightOp)
{
if (leftOp.getValue() == rightOp.getValue())
{
return true;
}
else
{
return false;
}
}
ostream& operator<<(ostream& out, const ComplexNumber& rho)
{
out << rho.getValue();
return out;
}
This is my driver code.
#include "ComplexNumbers.h"
int main()
{
// Create complex numbers to do arithmentic with
ComplexNumber cm1(1, 2);
ComplexNumber cm2(1, -2);
// test addition operator
ComplexNumber cm3 = cm1 + cm2;
cout << cm3 << endl;
// test subtraction operator
ComplexNumber cm4 = cm1 - cm2;
cout << cm4 << endl;
// test multiplication operator
ComplexNumber cm5 = cm1 * cm2;
cout << cm5 << endl;
// test division operator
ComplexNumber cm6 = cm1 / cm2;
cout << cm6 << endl;
// test assignment operator
cm6 = cm5;
cout << cm6 << endl;
// test comparison operator
if (cm1 == cm2)
cout << "\nThey are equal.\n";
else
cout << "\nThey are not equal.";
ComplexNumber cm8(1, 2);
if (cm1 == cm8)
cout << "\nThey are equal.\n";
else
cout << "\nThey are not equal.";
system("PAUSE");
return 0;
}
Here is the header file that is requested
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
class ComplexNumber
{
public:
//Constructor
//Purpose: Set the value of ComplexNumber to 0
//Parameters: None
//Returns: None
ComplexNumber();
//Parameterized Constructor
//Purpose: Set the value of ComplexNumber to the parameter
//Parameters: A double, and a double
//Returns: None
ComplexNumber(double, double);
//getValue
//Purpose:Getter for complexNumberValue
//Parameters: None
//Returns: the double value of object
double getValue() const;
//plusOverload
//Purpose: Overload the + operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the sum
ComplexNumber operator+(const ComplexNumber&) const;
//minusOverload
//Purpose: Overload the - operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the difference
ComplexNumber operator-(const ComplexNumber&) const;
//multiplicationOverload
//Purpose: Overload the * operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the product
ComplexNumber operator*(const ComplexNumber&) const;
//divisionOverload
//Purpose: Overload the / operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the quotient
ComplexNumber operator/(const ComplexNumber&) const;
//assignment
//Purpose: Overload the = operator
//Parameters: The two ComplexNumbers we want to assign
//Returns:
ComplexNumber operator=(const ComplexNumber&);
//comparison
//Purpose: Overload the == operator
//Parameters: The two ComplexNumbers we want to compare
//Returns:
bool operator==(const ComplexNumber&) const;
private:
double complexNumberValue;
double realNumberValue;
};
//Independant Functions
//streamInsertionOverload
//Purpose: Overload the stream insertion operator
//Parameters: The stream object to read from, and object to read to
//Returns:The stream
ostream& operator<<(const ostream&, const ComplexNumber&);
And here are my errors
Error 1 error LNK2019: unresolved external symbol "public: bool __thiscall ComplexNumber::operator==(class ComplexNumber const &)const " (??8ComplexNumber@@QBE_NABV0@@Z) referenced in function _main
Error 2 error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > const &,class ComplexNumber const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@ABV01@ABVComplexNumber@@@Z) referenced in function _main
Error 3 error LNK2019: unresolved external symbol "public: double __thiscall ComplexNumber::getValue(void)const " (?getValue@ComplexNumber@@QBENXZ) referenced in function "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class ComplexNumber const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVComplexNumber@@@Z)
Error 4 error LNK1120: 3 unresolved externals