MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.
return *this * rec_power(base, n-1);
comp t = *this * rec_power(base, n-1);
return t;
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
template <typename T>
class comp
{
public:
T im;
T re;
public:
// Constructor with parameters
comp(T re, T im)
{
this->im = im;
this->re = re;
}
// Default constructor
comp()
{
this->im = 0;
this->re = 0;
}
// Constructor with one param. Assuming that single parameter
// is the real part of the comple number
comp(T re)
{
this->re = re;
}
// Copy constructor
comp(const comp& c)
{
re = c.re;
im = c.im;
}
// Empty destructor
~comp() {};
comp& operator+(comp& second)
{
comp result;
result.re = this->re + second.re;
result.im = this->im + second.im;
return result;
}
// Multiplying a complex number by another complex number
comp& operator*(comp& second)
{
comp result;
result.re = this->re * second.re + (-1)* this->im * second.im;
result.im = this->re * second.im + this->im * second.re;
return result;
}
// Multiplying a complex number by another number number
comp& operator*(T n)
{
comp result;
result.re = this->re * n;
result.im = this->im * n;
return result;
}
T modulus()
{
return sqrt(this->re*this->re + this->im*this->im);
}
comp& it_power(int n)
{
comp result(1, 0);
for (int i = 1; i <= n; i++)
result = *this * result;
return result;
}
comp& rec_power(comp base, int n)
{
comp one(1, 0);
if (n == 0)
return one;
else if (n == 1)
return *this;
else
{
comp t = *this * rec_power(base, n-1);
return t;
// investigate why this does not work
//return *this * rec_power(base, n-1);
}
}
// Overloading input operator
friend std::ostream& operator<<(ostream& out, comp& c)
{
out << setprecision(2) << fixed
<< "(" << c.re << ", " << c.im
<< "i)" << endl;
return out;
}
// Overloading output operator
friend std::istream& operator>>(istream& in, comp& c)
{
in >> c.re ;
in >> c.im;
return in;
}
};
int main()
{
comp<double> myc(3, 2);
comp<double> myc2(1, 4);
// cin >> myc;
// cin >> myc2;
comp<double> r = myc + myc2;
comp<double> m = myc * myc;
cout << r;
cout << m;
cout << r.modulus() << endl;
cout << m.modulus() << endl;
cout << myc.it_power(6) << endl;
cout << myc.rec_power(myc, 6) << endl;
return 0;
}
return *this * rec_power(base, n-1);
comp t = *this * rec_power(base, n-1);
return t;
comp& it_power(int n)
{
comp result(1, 0);
for (int i = 1; i <= n; i++)
result = *this * result;
return result;
}
This will update the object on which the function is being called (the "this" pointer).
no. comp rec_power(comp base, int n) const{
if (n == 0)
return comp(1,0);
comp t = (*this) * rec_power(base, n-1);
return t;
}
you see for n == 1 the return value would be (*this) * (1,0) what is the same as (*this).// investigate why this does not worki would guess it will be accepted by the compiler if you changed the return type to value type.
//return *this * rec_power(base, n-1);
return *this * rec_power(base, n-1);
comp t = *this * rec_power(base, n-1);
return t;
comp rec_power(comp base, int n)
{
if (n == 0)
return comp(1,0);
comp t = (*this) * rec_power(base, n-1);
return t;
// investigate why this does not work
//return *this * rec_power(base, n-1);
}
C:\Complex2\main.cpp|98|error: no match for 'operator*' in '*(comp<double>*)this * comp<T>::rec_power(comp<T> , int) [with T = double; comp<T> = comp<double>](comp<double> ((*(const comp<double>*)(& base))), (n + -1))'|
comp& operator*(comp& second)the problem is not the dereference operator* but the multiplication operator*. for that you defined an operator function which actually is an operator*= , as it changes the 'this'. because of that definition the compiler doesn't accept a multiplication of *this in another member function.
// addition (add another comp to *this and return a temporary by value)
comp operator+(const comp & t) const { .. }
// multiplication (multiply another comp to *this and return a temporary by value)
comp operator*(const comp & t) const
{
comp temp;
temp.re = this->re * second.re + (-1)* this->im * second.im;
temp.im = this->re * second.im + this->im * second.re;
// here do not change 'this' but return temp by value
return temp;
}
// the operator*= would be a non-const function:
// multiplication + assignment
comp & operator*=(const comp & second)
{
comp temp;
temp.re = this->re * second.re + (-1)* this->im * second.im;
temp.im = this->re * second.im + this->im * second.re;
// here the this was modified
*this = temp;
return *this; // return a reference to modified this
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.