c++most-vexing-parse

complex class's default constructor doesn't work


According to http://www.cplusplus.com/reference/complex/complex/complex/, class complex<double> has a default construct of the form complex (double re = 0.0, double im = 0.0);. However, I don't know if this is really correct. Could anyone explain the strange behavior of the code below?

#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

}

Environment:

$ g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609

Compiling option:

$ g++ -Wall -std=c++98 <file name>

Solution

  • change:

    complex<double> cmp3(); //isn't the same as cmp3(0.0, 0.0) ?
    

    to

    complex<double> cmp3; //isn't the same as cmp3(0.0, 0.0) ?
    

    and it should work just fine

    It happends because compiler may assume that that is a function declaration