I am getting error while creating object as shown below using unique_ptr Error: Error conversion from Account * to non-scalar type std::unique_ptr
std::unique_ptr <Account> acc_ptr = new Account(100);
If I use raw pointer as below, there is no error
Account *acc_ptr = new Account(100);
Why is it so?
The std::unique_ptr constructor taking a pointer is explicit.
You need this:
std::unique_ptr <Account> acc_ptr(new Account(100));
Or, since C++14, use the better std::make_unique version:
auto acc_ptr = std::make_unique<Account>(100);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With