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.
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <stdexcept>
usingnamespace 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:
booloperator==(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&);
Okay so with that recommendation here is my new header file, but I am receiving other errors from my implementation code. I am not understanding what to place in the implementation file for this comparison operator, what I have now is giving me different errors.
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <stdexcept>
usingnamespace 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&);
private:
double complexNumberValue;
double realNumberValue;
};
//Independant Functions
//comparison
//Purpose: Overload the == operator
//Parameters: The two ComplexNumbers we want to compare
//Returns:
booloperator==(const ComplexNumber&, const ComplexNumber&);
//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&);
I included the wrong code, here is the updated code with the == out of the class. I included the new error in the post, but I don't understand how to clean up the definition of the << operator.
Reply okay I figured out how to clean up that definition, by removing the const call from the header file, those are now lined up. But I am not getting the output that I need, I am supposed to get a text file that reads:
2
4i
5
-0.6 + 0.8i
5
They are not equal.
They are equal.
Press any key to continue . . .
Mine reads:
2
0
1
1
1
They are equal.
They are equal.
Press any key to continue . . .
I am not sure why they are so different, from my expected results.
Your comparsion opeator compares only real parts.
Your output operator outputs only real part. Multiplication and division are incorrect too (They multiply real parts and just copy imaginary from left hand operand)
I know that I need to change my << overload to handle the output, but I don't understand what I need to change, I have studied the material listed above but am not certain how to change mine to accept that. This is the only hint I have to get the << to work correctly.
If the value of the real part is zero, do not output the zero, just output the value of the imaginary part, followed by the i.
If the value of the imaginary part is zero, do not output the zero, just output the value of the real part.
If both the real part and the imaginary part are zero, just output a zero.
If the imaginary part is negative your output should be of the form a - bi.
Just do what required:
Check if both real and imaginary parts are 0. If so, output 0.
Else check if real part 0. If not, output real part.
Now check if imaginary part is 0.
If not, output imaginary part and i.
ostream& operator<<(ostream& out, const ComplexNumber& rho)
{
if (rho.complexNumberValue && rho.complexNumberValue == 0)
{
out;//return zero
}
if (rho.realNumberValue == 0)
{
out << rho.complexNumberValue << "i";
return out;//return imaginary part followed by i
}
if (rho.complexNumberValue == 0)
{
out << rho.realNumberValue;
return out;//return real part
}
if (rho.complexNumberValue < 0)
{
out << (rho.realNumberValue - rho.complexNumberValue) << "i";
return out;//output should be in form a - bi
}
}