-----------------
prog2.cpp:36:33: error: declaration of ‘operator<<’ as non-function
prog2.cpp:36:32: error: expected ‘;’ before ‘(’ token
prog2.cpp: In function ‘int main()’:
prog2.cpp:49:12: error: no match for ‘operator<<’ in ‘std::cout << z’
------------------
My code is below, it is a simple complex number class:
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using namespace std;
- class complex
- {
- private:
- double re, im; //real and imaginary parts
- public:
- void set(double r, double i) {re = r; im = i;}
- complex() {set(0.0,0.0);}
- complex(double r, double i) {set(r,i);}
- complex(const complex &src)
- {re = src.re; im = src.im;}
- complex(double r) {set(r,0.0);}
- double get_re() const {return re;}
- double get_im() const {return im;}
- complex get_conj() const
- {complex c(re, -1.0*im); return c;}
- double get_mag_sq() const
- {double m = (re*re)+(im*im); return m;}
- complex add(const complex &other)
- {complex c(re+other.re,im+other.im); return c;}
- complex sub(const complex &other)
- {complex c(re-other.re,im-other.im); return c;}
- complex mult(const complex &other);
- complex div(const complex &other);
- complex operator+(const complex &other) {return add(other);}
- complex operator-(const complex &other) {return sub(other);}
- complex operator*(const complex &other) {return mult(other);}
- complex operator/(const complex &other) {return div(other);}
- bool operator==(const complex &other)
- {return re == other.re && im == other.im;}
- friend ostream &operator<<(osteam &os, complex &cplx);
- };
- int main()
- {
- complex x(5.0,6.9);
- complex y(3.0,4.0);
- complex z = x*y;
- z = z/y;
- cout << x.get_re() << "+" << x.get_im() << "i" << endl;
- cout << z.get_re() << "+" << z.get_im() << "i" << endl;
- cout << (x == y) << endl;
- cout << z << endl;
- return 0;
- }
- complex complex::mult(const complex &other)
- {
- complex c(re*other.re-im*other.im,re*other.im+im*other.re);
- return c;
- }
- complex complex::div(const complex &other)
- {
- complex c = (*this * other.get_conj())*(1/other.get_mag_sq());
- return c;
- }
- ostream &operator<<(ostream &os, complex &cplx)
- {
- os << cplx.re << "+" << cplx.im << "i";
- return os;
- }