Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的复数示例+;不起作用_C++_Version_Complex Numbers - Fatal编程技术网

C++ c+中的复数示例+;不起作用

C++ c+中的复数示例+;不起作用,c++,version,complex-numbers,C++,Version,Complex Numbers,来自()的简单示例代码不起作用,我不知道为什么 #include <iostream> #include <iomanip> #include <complex> #include <cmath> int main() { using namespace std::complex_literals; std::cout << std::fixed << std::setprecision(1);

来自()的简单示例代码不起作用,我不知道为什么

#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';
}
并获取错误

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';
                                           ^
它是有效的,但当我执行时

bash: ./complex_numbers_example: Permission denied
< C++ >我使用的版本

c++ (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
对于cpp,我使用的是版本

cpp (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609

在c++14中添加了复杂的文本,请参见

您需要使用以下工具进行编译:

c++ -o complex_numbers_example complex_numbers_example.cpp -std=c++14

不要尝试用<代码> CPP<代码>编译,即<强> C++ >强> P>强> R>强> P<强> ROSeOR,并生成预处理的C++文件,不可执行。< / P> < P>复杂文字被添加到C++ 14中,参见

您需要使用以下工具进行编译:

c++ -o complex_numbers_example complex_numbers_example.cpp -std=c++14

<>不要尝试用<代码> CPP<代码>编译,即<强> c>强> P>强> >强> P< /强> RoSoor,并产生预处理的C++文件,而不是可执行文件。

要求C++ 14:“你确实做了一个很好的问题:A,你从哪里得到代码,B,你包括一个完整的,自包含示例,c)您发布了特定的错误,d)您给出了编译器版本和平台。问题:该示例似乎与您的G++5.4版本不兼容。@SM4在哪里可以找到我需要的版本?那么C++、GCC、CPP?哪一个最好用?安装新版本难吗?@JanSE如果使用
c++-o complex\u numbers\u example complex\u numbers\u example.cpp-std=c++14
,“在哪里可以找到我需要的版本?”:使用联机编译器链接(上面)并更改编译器版本。将编译器更改为早期版本,直到找到编译/不编译边界。回答4.9.0及以上。需要C++14 live:@Jan SE-你问的问题真的做得很好:a)你引用了代码的来源,b)你包含了一个完整的自包含示例,C)你发布了具体的错误,d)你给出了你的编译器版本和平台。问题:该示例似乎与您的G++5.4版本不兼容。@SM4在哪里可以找到我需要的版本?那么C++、GCC、CPP?哪一个最好用?安装新版本难吗?@JanSE如果使用
c++-o complex\u numbers\u example complex\u numbers\u example.cpp-std=c++14
,“在哪里可以找到我需要的版本?”:使用联机编译器链接(上面)并更改编译器版本。将编译器更改为早期版本,直到找到编译/不编译边界。回答4.9.0及以上。
c++ -o complex_numbers_example complex_numbers_example.cpp -std=c++14