Reputation: 19
Im trying to find my way around defining structures, and coded this to define a complex number. Ive already had success working with them, adding multiplying e.g., but I dont know how to get the absolute value of my complex number converted into a double, my compiler keeps telling me it doesnt know how to convert it, even though the complex struct is literally made of 2 doubles..
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct Complex_Number {
double re, im;
};
void print(const Complex_Number& w) //my print function for complex numbers
{
cout << '(' << w.re << " + i*" << w.im << ')';
}
double abs(const Complex_Number& z )
{
z = z.re + z.im;
abs = sqrt((z.re*z.re)+(z.im*z.im));
return abs;
}
int main()
{
return 0;
}
Upvotes: 0
Views: 751
Reputation: 35154
I don't know what you intend with z = z.re + z.im
; The second part just needs to be assigned to a variable or returned directly. So write...
double abs(const Complex_Number& z ) {
double ret = sqrt((z.re*z.re)+(z.im*z.im));
return ret;
}
or
double abs(const Complex_Number& z ) {
return sqrt((z.re*z.re)+(z.im*z.im));
}
Assigning something to the "function name" as in your code, i.e. abs = sqrt((z.re*z.re)+(z.im*z.im))
, is not valid C++ code.
Upvotes: 1