I am trying to do numerical calculations with C++. Here is the sample code
#include <complex>
using namespace std;
complex<double> complexDo(float a, float b){
return (a+b,a-b);
}
int main(){
cout << "complexDo="<<complexDo(3,2) <<'\n';
return 0;
}
The terminal will show after compiling
complexDo=(1,0)
But I expect to appear like (5, 1), and what is the problem here? Or this way of writing is not valid in C++?
>Solution :
The expression (a+b,a-b) is equivalent to (a-b) because it’s just a parenthesized use of the common comma expression.
To create an object you must use curly-braces {} as in
return {a+b,a-b};