complex class 的默认构造函数不起作用
complex class's default constructor doesn't work
根据 http://www.cplusplus.com/reference/complex/complex/complex/,class complex<double>
具有 complex (double re = 0.0, double im = 0.0);
形式的默认结构。但是,我不知道这是否真的正确。谁能解释下面代码的奇怪行为?
#include <iostream>
#include <complex>
#include <typeinfo>
using namespace std;
int main() {
complex<double> cmp1(1, 2);
complex<double> cmp2(1); //isn't the same as cmp2(1, 0.0) ?
complex<double> cmp3(); //isn't the same as cmp3(0.0, 0.0) ?
cout << cmp1.real() << "\n"; //=> 1
cout << cmp1.imag() << "\n"; //=> 2
cout << cmp2.real() << "\n"; //=> 1
cout << cmp2.imag() << "\n"; //=> 0
//cout << cmp3.real() << "\n"; //error (why?) //mark1
//cout << cmp3.imag() << "\n"; //error (why?)
/* error message output by `mark1`: */
/* request for member ‘real’ in ‘cmp3’, which is of */
/* non-class type ‘std::complex<double>()’ */
cout << cmp3 << "\n"; //=> 1 (why?)
cout << typeid(cmp1).name() << "\n"; //=> St7complexIdE
cout << typeid(cmp2).name() << "\n"; //=> St7complexIdE
cout << typeid(cmp3).name() << "\n"; //=> FSt7complexIdEvE
}
环境:
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
编译选项:
$ g++ -Wall -std=c++98 <file name>
变化:
complex<double> cmp3(); //isn't the same as cmp3(0.0, 0.0) ?
至
complex<double> cmp3; //isn't the same as cmp3(0.0, 0.0) ?
它应该工作得很好
这是因为编译器可能认为这是一个函数声明
根据 http://www.cplusplus.com/reference/complex/complex/complex/,class complex<double>
具有 complex (double re = 0.0, double im = 0.0);
形式的默认结构。但是,我不知道这是否真的正确。谁能解释下面代码的奇怪行为?
#include <iostream>
#include <complex>
#include <typeinfo>
using namespace std;
int main() {
complex<double> cmp1(1, 2);
complex<double> cmp2(1); //isn't the same as cmp2(1, 0.0) ?
complex<double> cmp3(); //isn't the same as cmp3(0.0, 0.0) ?
cout << cmp1.real() << "\n"; //=> 1
cout << cmp1.imag() << "\n"; //=> 2
cout << cmp2.real() << "\n"; //=> 1
cout << cmp2.imag() << "\n"; //=> 0
//cout << cmp3.real() << "\n"; //error (why?) //mark1
//cout << cmp3.imag() << "\n"; //error (why?)
/* error message output by `mark1`: */
/* request for member ‘real’ in ‘cmp3’, which is of */
/* non-class type ‘std::complex<double>()’ */
cout << cmp3 << "\n"; //=> 1 (why?)
cout << typeid(cmp1).name() << "\n"; //=> St7complexIdE
cout << typeid(cmp2).name() << "\n"; //=> St7complexIdE
cout << typeid(cmp3).name() << "\n"; //=> FSt7complexIdEvE
}
环境:
$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
编译选项:
$ g++ -Wall -std=c++98 <file name>
变化:
complex<double> cmp3(); //isn't the same as cmp3(0.0, 0.0) ?
至
complex<double> cmp3; //isn't the same as cmp3(0.0, 0.0) ?
它应该工作得很好
这是因为编译器可能认为这是一个函数声明