the simple example code from (https://en.cppreference.com/w/cpp/numeric/complex) is not working and I don't know why?
#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>
int main()
{
using namespace std::complex_literals;
std::cout << std::fixed << std::setprecision(1);
std::complex<double> z1 = 1i * 1i; // imaginary unit squared
std::cout << "i * i = " << z1 << '\n';
std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
std::cout << "pow(i, 2) = " << z2 << '\n';
double PI = std::acos(-1);
std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
std::cout << "exp(i * pi) = " << z3 << '\n';
std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';
}
I am compling with
c++ -o complex_numbers_example complex_numbers_example.cpp -std=c++11
and get the error
complex_numbers_example.cpp: In function ‘int main()’:
complex_numbers_example.cpp:8:26: error: ‘complex_literals’ is not a namespace-name
using namespace std::complex_literals;
^
complex_numbers_example.cpp:8:42: error: expected namespace-name before ‘;’ token
using namespace std::complex_literals;
^
complex_numbers_example.cpp:11:31: error: unable to find numeric literal operator ‘operator""i’
std::complex<double> z1 = 1i * 1i; // imaginary unit squared
^
complex_numbers_example.cpp:11:31: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
complex_numbers_example.cpp:11:36: error: unable to find numeric literal operator ‘operator""i’
std::complex<double> z1 = 1i * 1i; // imaginary unit squared
^
complex_numbers_example.cpp:11:36: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
complex_numbers_example.cpp:14:40: error: unable to find numeric literal operator ‘operator""i’
std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
^
complex_numbers_example.cpp:14:40: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
complex_numbers_example.cpp:18:40: error: unable to find numeric literal operator ‘operator""i’
std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
^
complex_numbers_example.cpp:18:40: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
complex_numbers_example.cpp:21:36: error: unable to find numeric literal operator ‘operator""i’
std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
^
complex_numbers_example.cpp:21:36: note: use -std=gnu++11 or -fext-numeric-literals to enable more built-in suffixes
complex_numbers_example.cpp:22:43: error: ‘z5’ was not declared in this scope
std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';
^
When I try to compile with
cpp -o complex_numbers_example complex_numbers_example.cpp
it works, but when executing I get
bash: ./complex_numbers_example: Permission denied
For c++ I am using version
c++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
and for cpp I am using version
cpp (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Complex literals were added in c++14, see https://en.cppreference.com/w/cpp/numeric/complex/operator%22%22i
You need to compile with:
c++ -o complex_numbers_example complex_numbers_example.cpp -std=c++14
Don't try to compile with cpp
, that is the C Pre Processor and will produce a preprocessed c++ file not an executable.