Converting a defined structure into double (complex numbers) in c++











up vote
0
down vote

favorite












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;




}









share|improve this question


















  • 1




    abs is the name of the function - i.e. a function pointer - you need to create a double variable.
    – Neil Butterworth
    Nov 11 at 19:42












  • See std::complex.
    – Thomas Matthews
    Nov 11 at 20:22










  • As @ThomasMatthews mentions, you should check out your local std::complex<double> to see how the interfaces can be defined by your standard library.
    – Bo R
    Nov 11 at 20:44















up vote
0
down vote

favorite












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;




}









share|improve this question


















  • 1




    abs is the name of the function - i.e. a function pointer - you need to create a double variable.
    – Neil Butterworth
    Nov 11 at 19:42












  • See std::complex.
    – Thomas Matthews
    Nov 11 at 20:22










  • As @ThomasMatthews mentions, you should check out your local std::complex<double> to see how the interfaces can be defined by your standard library.
    – Bo R
    Nov 11 at 20:44













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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;




}









share|improve this question













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;




}






c++ struct double complex-numbers






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 19:39









Henryk5161

43




43








  • 1




    abs is the name of the function - i.e. a function pointer - you need to create a double variable.
    – Neil Butterworth
    Nov 11 at 19:42












  • See std::complex.
    – Thomas Matthews
    Nov 11 at 20:22










  • As @ThomasMatthews mentions, you should check out your local std::complex<double> to see how the interfaces can be defined by your standard library.
    – Bo R
    Nov 11 at 20:44














  • 1




    abs is the name of the function - i.e. a function pointer - you need to create a double variable.
    – Neil Butterworth
    Nov 11 at 19:42












  • See std::complex.
    – Thomas Matthews
    Nov 11 at 20:22










  • As @ThomasMatthews mentions, you should check out your local std::complex<double> to see how the interfaces can be defined by your standard library.
    – Bo R
    Nov 11 at 20:44








1




1




abs is the name of the function - i.e. a function pointer - you need to create a double variable.
– Neil Butterworth
Nov 11 at 19:42






abs is the name of the function - i.e. a function pointer - you need to create a double variable.
– Neil Butterworth
Nov 11 at 19:42














See std::complex.
– Thomas Matthews
Nov 11 at 20:22




See std::complex.
– Thomas Matthews
Nov 11 at 20:22












As @ThomasMatthews mentions, you should check out your local std::complex<double> to see how the interfaces can be defined by your standard library.
– Bo R
Nov 11 at 20:44




As @ThomasMatthews mentions, you should check out your local std::complex<double> to see how the interfaces can be defined by your standard library.
– Bo R
Nov 11 at 20:44












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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.






share|improve this answer

















  • 1




    Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
    – Henryk5161
    Nov 11 at 19:51






  • 1




    Was just waiting for the option to be enabled, thx :)
    – Henryk5161
    Nov 11 at 20:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252484%2fconverting-a-defined-structure-into-double-complex-numbers-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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.






share|improve this answer

















  • 1




    Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
    – Henryk5161
    Nov 11 at 19:51






  • 1




    Was just waiting for the option to be enabled, thx :)
    – Henryk5161
    Nov 11 at 20:17















up vote
1
down vote



accepted










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.






share|improve this answer

















  • 1




    Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
    – Henryk5161
    Nov 11 at 19:51






  • 1




    Was just waiting for the option to be enabled, thx :)
    – Henryk5161
    Nov 11 at 20:17













up vote
1
down vote



accepted







up vote
1
down vote



accepted






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 19:44









Stephan Lechner

25.3k21839




25.3k21839








  • 1




    Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
    – Henryk5161
    Nov 11 at 19:51






  • 1




    Was just waiting for the option to be enabled, thx :)
    – Henryk5161
    Nov 11 at 20:17














  • 1




    Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
    – Henryk5161
    Nov 11 at 19:51






  • 1




    Was just waiting for the option to be enabled, thx :)
    – Henryk5161
    Nov 11 at 20:17








1




1




Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
– Henryk5161
Nov 11 at 19:51




Whoah thanks for that.. i have spent too much time today that i literally blocked myself from seeing this obvious one.. i really appreciate your help!
– Henryk5161
Nov 11 at 19:51




1




1




Was just waiting for the option to be enabled, thx :)
– Henryk5161
Nov 11 at 20:17




Was just waiting for the option to be enabled, thx :)
– Henryk5161
Nov 11 at 20:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252484%2fconverting-a-defined-structure-into-double-complex-numbers-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."