# How do I Overload these operators in this program?

• 04-23-2005
How do I Overload these operators in this program?
I have a program in which you have to overload the operators (all under a class called ComplexNum) >>, <<, +, -, /, and *. My program is that I must take two sets of numbers, each set having two numbers, and manipulate each with these operators. But here is the kicker, one of the numbers is an imaginary number. Ok, here is an example:
---------------
Input file:
2.5 -2.2
1.0 1.0
Output (what it is suppose to be)
A = (2.5) + (-2.2)i
B = (1.0) + (1.0)i

A + B = (3.5) + (-1.2)i ....and so on
---------------
Ok, I have been given for example, what to do with the overloaded assignment operator, such as
>> extracts two parts of the complex number (or)
<< inserts the complex number with (a) + (b)i format (or)
+ is (a + c) + i(b+d)
* is (ac-bd) + i(bc + ad)
....so on

But how do I implent or do any of this? I was not instructed very well, and any help with the coding would be apreciated. Thanks,
aaron
• 04-23-2005
hk_mp5kpdw
Code:

```class ComplexNum {     double real, imaginary; public:     // Constructor     ComplexNum( double realval = 0.0, double imagval = 0.0 ) : real(realval), imaginary(imagval) {}     // Copy constructor     ComplexNum( const ComplexNum& rhs )     {         real = rhs.real;         imaginary = rhs.imaginary;     }     friend ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs );     friend ostream& operator<<(ostream& out,const ComplexNum& rhs ); }; ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs ) {     return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary); } ostream& operator<<(ostream& out,const ComplexNum& rhs ) {     return out << "(" << rhs.real << ") + (" << rhs.imaginary << ")i"; } int main() {     ComplexNum A(2.5,-2.2), B(1.0,1.0), C;     C = A + B;     cout << "A is: " << A << endl;     cout << "B is: " << B << endl;     cout << "C is: " << C << endl;     return 0; }```
Output:
Code:

```A is: (2.5) + (-2.2)i B is: (1) + (1)i C is: (3.5) + (-1.2)i```
You can do the other operators yourself I'm sure.
• 04-23-2005
Hey, cool. Thanks a lot for your help!!!
• 04-23-2005
Quick question, I am going to be reading from an input file. How do I read two numbers from the input file and put them both into A like this? I belive this is why I have to overload the >> function?
• 04-23-2005
hk_mp5kpdw
I would guess you are supposed to implement the operator>> in order to read the numbers from your input file. Try and have a go at it.

Code:

```class ComplexNum {     ... public:     ...     friend istream& operator>>(istream& is, ComplexNum& rhs ); }; istream& operator>>(istream& is, ComplexNum& rhs ) {     // Figure out what to put here }```
• 04-23-2005
7stud
cross posted.
• 04-24-2005
Ok, I am now getting these errors with this code:
Code:

```#include <fstream.h> #include <ostream.h> class ComplexNum {   public:       float real;       float imaginary;       ComplexNum( double realval = 0.0, double imagval = 0.0 ) : real(realval), imaginary(imagval) {}       ComplexNum(const ComplexNum& rhs)       {         real = rhs.real;         imaginary = rhs.imaginary;       }       friend ostream& operator<<(ostream& outFile, const ComplexNum&                                   rhs);       friend istream& operator>>(istream& inFile, ComplexNum& rhs);       friend ComplexNum operator+(ostream& outFile, const ComplexNum&                                   rhs); }; ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs) {   return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary); } ostream& operator<<(ostream& outFile, const ComplexNum& rhs) {   return outFile << "(" << rhs.real << ") + (" << rhs.imaginary << ")i"; }```
Now, these errors:
/tmp/cclNvRtt.o(.text+0x0): In function `operator+(ComplexNum const&, ComplexNum const&)':
: multiple definition of `operator+(ComplexNum const&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x0): first defined here
/tmp/cclNvRtt.o(.text+0x44): In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)':
: multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x44): first defined here
collect2: ld returned 1 exit status

Any ideas why??
• 04-24-2005
hk_mp5kpdw
Quote:

Ok, I am now getting these errors with this code:
Code:

```#include <fstream.h>  // Use <fstream> instead #include <ostream.h>  // Use <iostream> instead using namespace std;  // Add this class ComplexNum { public:     ...     friend ostream& operator<<(ostream& outFile, const ComplexNum& rhs);     friend istream& operator>>(istream& inFile, ComplexNum& rhs);     friend ComplexNum operator+(ostream& outFile, const ComplexNum&                                   rhs); }; ComplexNum operator+(const ComplexNum& lhs, const ComplexNum& rhs) {   return ComplexNum(lhs.real+rhs.real, lhs.imaginary+rhs.imaginary); } ostream& operator<<(ostream& outFile, const ComplexNum& rhs) {   return outFile << "(" << rhs.real << ") + (" << rhs.imaginary << ")i"; }```
Now, these errors:
/tmp/cclNvRtt.o(.text+0x0): In function `operator+(ComplexNum const&, ComplexNum const&)':
: multiple definition of `operator+(ComplexNum const&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x0): first defined here
/tmp/cclNvRtt.o(.text+0x44): In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)':
: multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, ComplexNum const&)'
/tmp/ccCuUZWQ.o(.text+0x44): first defined here
collect2: ld returned 1 exit status

Any ideas why??

Your definition of operator+ does not match the declaration of it. These need to be the same. The operator+ function should take two const ComplexNum objects as arguments. What is that ostream object doing there?

You really should be using the newer headers (the ones without the .h extension). I.e. fstream instead of fstream.h and so on. As far as the ostream header, you are going to need the istream header as well since you are also going to be dealing with input streams so you might as well just include the iostream header which includes everything you need for both input and output streams. Using the newer headers means you will have to deal with namespaces, at this stage it is probably easiest to just put a using namespace std; line after all your header statements.

Don't know specifically what might be causing the problem with the operator<< function. Make the other fixes and see if you still get the error.
• 04-24-2005