ligin pc
ligin pc

Reputation: 31

Unique pointer error while object construction in C++

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?

Upvotes: 3

Views: 538

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117178

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);

Upvotes: 6

Related Questions