| | |
error in overloaded << and >>
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 4
Reputation:
Solved Threads: 0
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.
Here is the .cpp file the errors are actually in.
Thank you in advance.
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) - #include <iostream>
- using namespace std;
- class Complex
- {
- public:
- Complex();
- Complex(double real, double imaginary);
- Complex(double real);
- const Complex operator +(const Complex& value) const;
- const Complex operator -(const Complex& value) const;
- const Complex operator -() const;
- const Complex operator *(const Complex& value) const;
- friend ostream& operator <<(ostream& outputStream, const Complex value);
- friend istream& operator >>(istream& inputStream, Complex value);
-
- private:
- double real;
- double imaginary;
- };
Here is the .cpp file the errors are actually in.
C++ Syntax (Toggle Plain Text) - #include <iostream>
- #include <math.h>
- #include <cstdlib>
- using namespace std;
- #include "Complex.h"
- 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& secondValue) const
- {
- double realSum, imaginarySum;
- realSum = real + secondValue.real;
- imaginarySum = imaginary + secondValue.imaginary;
-
- return Complex(realSum, imaginarySum);
- }
- const Complex Complex::operator -(const Complex& 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.
- // [url]http://mathworld.wolfram.com/ComplexMultiplication.html[/url]
- const Complex Complex::operator *(const Complex& secondValue) const
- {
- double realProduct, imaginaryProduct;
- realProduct = real*secondValue.real-imaginary*secondValue.imaginary;
- imaginaryProduct = real*secondValue.imaginary-imaginary*secondValue.real;
-
- return Complex(realProduct, imaginaryProduct);
- }
- ostream& operator <<(ostream& outputStream, const Complex& value)
- {
- outputStream << real;
- if (imaginary < 0)
- outputStream << "-";
- else
- outputStream << "+";
- outputStream << imaginary << "i";
-
- return outputStream;
- }
- istream& operator >>(istream& inputStream, const Complex& value)
- {
- char sign, i;
- inputStream >> real >> sign >> imaginary >> i;
- if (sign == '-')
- imaginary = -imaginary;
-
- return inputStream;
- }
Thank you in advance.
![]() |
Similar Threads
- viewing contents of C++ objects in visual studio debugger (C++)
- The Onion: Myspace outage leaves millions friendless (Geeks' Lounge)
- i need help (C#)
- Formatting Strings (C#)
- help with creating and calling a function (C++)
- E-Mail error Message (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Which library is used for graphics in VC++
- Next Thread: Who willing to teach Programming?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






