943,962 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 935
  • C++ RSS
Mar 26th, 2007
1

error in overloaded << and >>

Expand Post »
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.
  
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. class Complex
  4. {
  5. public:
  6. Complex();
  7. Complex(double real, double imaginary);
  8. Complex(double real);
  9. const Complex operator +(const Complex& value) const;
  10. const Complex operator -(const Complex& value) const;
  11. const Complex operator -() const;
  12. const Complex operator *(const Complex& value) const;
  13. friend ostream& operator <<(ostream& outputStream, const Complex value);
  14. friend istream& operator >>(istream& inputStream, Complex value);
  15.  
  16. private:
  17. double real;
  18. double imaginary;
  19. };

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

  
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cstdlib>
  4. using namespace std;
  5. #include "Complex.h"
  6. Complex::Complex(): real(0), imaginary(0)
  7. { }
  8. Complex::Complex(double realPart, double imaginaryPart): real(realPart), imaginary(imaginaryPart)
  9. { }
  10. Complex::Complex(double realPart): real(realPart), imaginary(0)
  11. { }
  12. const Complex Complex::operator +(const Complex& secondValue) const
  13. {
  14. double realSum, imaginarySum;
  15. realSum = real + secondValue.real;
  16. imaginarySum = imaginary + secondValue.imaginary;
  17.  
  18. return Complex(realSum, imaginarySum);
  19. }
  20. const Complex Complex::operator -(const Complex& secondValue) const
  21. {
  22. double realDiff, imaginaryDiff;
  23. realDiff = real - secondValue.real;
  24. imaginaryDiff = imaginary - secondValue.imaginary;
  25.  
  26. return Complex(realDiff, imaginaryDiff);
  27. }
  28. const Complex Complex::operator -() const
  29. {
  30. return Complex(-real, -imaginary);
  31. }
  32. // just a reminder that complex multiplication is defined as:
  33. // (a+bi)(c+di) = (ac-bd) + (ad-bc)i.
  34. // [url]http://mathworld.wolfram.com/ComplexMultiplication.html[/url]
  35. const Complex Complex::operator *(const Complex& secondValue) const
  36. {
  37. double realProduct, imaginaryProduct;
  38. realProduct = real*secondValue.real-imaginary*secondValue.imaginary;
  39. imaginaryProduct = real*secondValue.imaginary-imaginary*secondValue.real;
  40.  
  41. return Complex(realProduct, imaginaryProduct);
  42. }
  43. ostream& operator <<(ostream& outputStream, const Complex& value)
  44. {
  45. outputStream << real;
  46. if (imaginary < 0)
  47. outputStream << "-";
  48. else
  49. outputStream << "+";
  50. outputStream << imaginary << "i";
  51.  
  52. return outputStream;
  53. }
  54. istream& operator >>(istream& inputStream, const Complex& value)
  55. {
  56. char sign, i;
  57. inputStream >> real >> sign >> imaginary >> i;
  58. if (sign == '-')
  59. imaginary = -imaginary;
  60.  
  61. return inputStream;
  62. }

Thank you in advance.
Similar Threads
Reputation Points: 23
Solved Threads: 0
Newbie Poster
MattTobin is offline Offline
4 posts
since Mar 2007
Mar 26th, 2007
0

Re: error in overloaded << and >>

line 45 should be like this: (use the value object)
outputStream << value.real;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Mar 26th, 2007
0

Re: error in overloaded << and >>

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
Last edited by MattTobin; Mar 26th, 2007 at 7:29 am.
Reputation Points: 23
Solved Threads: 0
Newbie Poster
MattTobin is offline Offline
4 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Which library is used for graphics in VC++
Next Thread in C++ Forum Timeline: Who willing to teach Programming?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC