954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

error in overloaded << and >>

hello again,
I've run into a problem in overloading << and >>. Within the definition when compiling its saying that 'real' and 'imaginary' are undeclared, these errors are in lines 45(real), 46(imaginary) and 57(both). Whats wrong here? And if you could please let me know how to correct this.

Heres the header file.

<code>
&lt;pre&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;
class Complex
{
      public:
            Complex();
            Complex(double real, double imaginary);
            Complex(double real);
            const Complex operator +(const Complex&amp;amp; value) const;
            const Complex operator -(const Complex&amp;amp; value) const;
            const Complex operator -() const;
            const Complex operator *(const Complex&amp;amp; value) const;
            friend ostream&amp;amp; operator &amp;lt;&amp;lt;(ostream&amp;amp; outputStream, const Complex value);
            friend istream&amp;amp; operator &amp;gt;&amp;gt;(istream&amp;amp; inputStream, Complex value);
 
      private:
            double real;
            double imaginary;               
};&lt;/code&gt;&lt;/pre&gt;</code>


Here is the .cpp file the errors are actually in.

<code>
&lt;pre&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;math.h&amp;gt;
#include &amp;lt;cstdlib&amp;gt;
using namespace std;
#include &amp;quot;Complex.h&amp;quot;
Complex::Complex(): real(0), imaginary(0)
{                   }
Complex::Complex(double realPart, double imaginaryPart): real(realPart), imaginary(imaginaryPart)
{                       }
Complex::Complex(double realPart): real(realPart), imaginary(0)
{                       }
const Complex Complex::operator +(const Complex&amp;amp; secondValue) const
{
      double realSum, imaginarySum;
      realSum = real + secondValue.real;
      imaginarySum = imaginary + secondValue.imaginary;
 
      return Complex(realSum, imaginarySum);
}
const Complex Complex::operator -(const Complex&amp;amp; secondValue) const
{
      double realDiff, imaginaryDiff;
      realDiff = real - secondValue.real;
      imaginaryDiff = imaginary - secondValue.imaginary;
 
      return Complex(realDiff, imaginaryDiff);
}
const Complex Complex::operator -() const
{
      return Complex(-real, -imaginary);
}
// just a reminder that complex multiplication is defined as:
// (a+bi)(c+di) = (ac-bd) + (ad-bc)i.
//  &amp;lt;a href=&amp;quot;http://mathworld.wolfram.com/ComplexMultiplication.html&amp;quot;&amp;gt;http://mathworld.wolfram.com/ComplexMultiplication.html&amp;lt;/a&amp;gt; 
const Complex Complex::operator *(const Complex&amp;amp; secondValue) const
{
      double realProduct, imaginaryProduct;
      realProduct = real*secondValue.real-imaginary*secondValue.imaginary;
      imaginaryProduct = real*secondValue.imaginary-imaginary*secondValue.real;
 
      return Complex(realProduct, imaginaryProduct);
}
ostream&amp;amp; operator &amp;lt;&amp;lt;(ostream&amp;amp; outputStream, const Complex&amp;amp; value)
{
      outputStream &amp;lt;&amp;lt; real;
      if (imaginary &amp;lt; 0)
         outputStream &amp;lt;&amp;lt; &amp;quot;-&amp;quot;;
      else
         outputStream &amp;lt;&amp;lt; &amp;quot;+&amp;quot;;
      outputStream &amp;lt;&amp;lt; imaginary &amp;lt;&amp;lt; &amp;quot;i&amp;quot;;
 
      return outputStream;
}
istream&amp;amp; operator &amp;gt;&amp;gt;(istream&amp;amp; inputStream, const Complex&amp;amp; value)
{
      char sign, i;
      inputStream &amp;gt;&amp;gt; real &amp;gt;&amp;gt; sign &amp;gt;&amp;gt; imaginary &amp;gt;&amp;gt; i;
      if (sign == '-')
          imaginary = -imaginary;
 
      return inputStream;
}&lt;/code&gt;&lt;/pre&gt;</code>


Thank you in advance.

MattTobin
Newbie Poster
4 posts since Mar 2007
Reputation Points: 23
Solved Threads: 0
 

line 45 should be like this: (use the value object)
outputStream << value.real;

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok, i had tried that and then i got an error because real and imaginary are private
/edit
i found it, thx though, in the header the '&' is missing from both << and >> and there is a const in the .cpp file that shouldnt be there. sorry =P

MattTobin
Newbie Poster
4 posts since Mar 2007
Reputation Points: 23
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You