根据http://www.cplusplus.com/reference/complex/complex/complex/,类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>发布于 2018-03-21 11:25:50
改变:
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) ?它应该运作得很好
之所以会发生这种情况,是因为编译器可能假定这是一个函数声明。
https://stackoverflow.com/questions/49404859
复制相似问题