error in overloaded << and >>

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 4
Reputation: MattTobin is an unknown quantity at this point 
Solved Threads: 0
MattTobin MattTobin is offline Offline
Newbie Poster

error in overloaded << and >>

 
1
  #1
Mar 26th, 2007
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.
  
  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.

  
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: error in overloaded << and >>

 
0
  #2
Mar 26th, 2007
line 45 should be like this: (use the value object)
outputStream << value.real;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: MattTobin is an unknown quantity at this point 
Solved Threads: 0
MattTobin MattTobin is offline Offline
Newbie Poster

Re: error in overloaded << and >>

 
0
  #3
Mar 26th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC