简体   繁体   中英

Why am I not able to use implicit initialization?

This is what I have:

Complex.h
class Complex
{
public:
    Complex(double initialRealPart, double initialImaginaryPart = 0.0)
    : realPart(initialRealPart), imaginaryPart(initialImaginaryPart)
    {
    }
    Complex() { }

private:
    double realPart;
    double imaginaryPart;
};


int main()
{
    Complex c = 4.5;
}

Isn't implicit initialization supposed to work in this situation?

EDIT: Oops, I actually had realPart and imaginaryPart as doubles in my code. It's still not working, though.

When I try to initialize implicitly, it says no suitable conversion from double to Complex.

EDIT2: I also had a Complex() constructor that didn't take in any arguments in my Complex.h

Probably your mistake is that you made realPart and imaginaryPart to be of type int

Otherwise this code has no errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM