Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique pointer error while object construction in C++

Tags:

c++

unique-ptr

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?

like image 978
ligin pc Avatar asked Nov 17 '25 05:11

ligin pc


1 Answers

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);
like image 170
Ted Lyngmo Avatar answered Nov 19 '25 18:11

Ted Lyngmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!