Hello everyone...i'm not sure why this isn't working...
what i'm trying to do is do the following:
I have it so my class will work if i just use 2 of the same types likeCode:Complex<int> a(3,10); Complex<float> b(3.4,30.2); cout << a + b << endl;
That will run fine.Code:Complex<int> a(3,2); Complex<int> b(20,3); cout << a + b << endl;
Well here is my code:
Code:// Complex.h #pragma once #include <iostream> using namespace std; //you can have template <typename T, typename B> // template <typename T, SIZE> size is now a constant template <typename T> class Complex { public: // default constructor Complex() { Real = Imaginary = 0.0; } Complex(T real, T imag) { this->Real = real; this->Imaginary = imag; } // copy constructor Complex(const Complex& c) { Real = c.Real; Imaginary = c.Imaginary; } T Real; T Imaginary; // overloading + Complex operator +(const Complex& c) { return Complex(Real + c.Real, Imaginary + c.Imaginary); } template <typename T> Complex<T> operator +(const Complex<T> & c) { return Complex(Real + c.Real, Imaginary + c.Imaginary); } Complex operator +(const T b) { return Complex(Real + b, Imaginary); } // overload - Complex operator-(const Complex & c) { return Complex(Real - c.Real, Imaginary - c.Imaginary); } // overloading = template <typename T> Complex<T> operator=(const Complex<T> & c) { return Complex(Real = c.Real, Imaginary = c.Imaginary); } template <typename T> Complex<T>& operator+=(const Complex<T> & c) { Real += c.Real; Imaginary += c.Imaginary; return *this; //you take a value from a source and you assign //it to the destination, you destroied the oringal data //so you must return a oginal value to it } //allow a floating point to be added to the complex /*Complex& operator+=(const T real) { Real += real; return *this; }*/ /*allow a floating point to be added with + Complex operator+ (T num) { return Complex(Real+num, Imaginary ); }*/ Complex operator * (const Complex &c) { return Complex(this->Real*c.Real - this->Imaginary*c.Imaginary, this->Real*c.Imaginary + this->Imaginary*c.Real); } template <typename T> Complex<T>* FFT(Complex<T>* data, int size) { return data; } }; template <typename T>ostream& operator << (ostream& out, const Complex<T>& c) { out << "(" << c.Real << "," << c.Imaginary << ")"; return out; }
I'm getting the following errror and its pointing to this chunk of code:
its saying:Code:template <typename T> Complex<T> operator +(const Complex<T> & c) { return Complex(Real + c.Real, Imaginary + c.Imaginary); }
I've looked up a few tutorials and have no idea what i'm not doing right, i nkow i have to do a specialized function if i want to add 2 different class types <int> and <float> but i don't see what i'm messing up on..Code:(55) : error C2664: 'Complex<T>::Complex(const Complex<T> &)' : cannot convert parameter 1 from 'Complex<T>' to 'const Complex<T> &' 1> with 1> [ 1> T=float 1> ] 1> and 1> [ 1> T=long double 1> ] 1> and 1> [ 1> T=float 1> ] 1> Reason: cannot convert from 'Complex<T>' to 'const Complex<T>' 1> with 1> [ 1> T=long double 1> ] 1> and 1> [ 1> T=float 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Thanks!




Reply With Quote