-3

I currently try to initialize the following array, Spot is a class that is defined elsewhere:

static const int WIDTH = 7; static const int HEIGHT = 6; std::array<std::array<std::unique_ptr<Spot>, WIDTH>, HEIGHT> field; 

When trying to initialize it:

 for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { field.at(i).at(j) = std::make_unique<Spot>(new Spot()); } } 

it states that Spot* cannot be converted to const Spot &, which makes pretty much sense.

Google is not really helpful here, as the questions either deal with

std::unique_ptr<T> [] or std::uniqe_ptr<std::array<T>> but not with std::array<std::unique_ptr<T>> 

So, how do you achieve that? Is that even a thing? Or are you not supposed to use std::arrays with smart pointers at all?

3
  • 1
    std::array is irrelevant. You can use a plain unique_ptr<Spot> and have the same error. Commented May 27, 2018 at 7:13
  • No, the link is not about whether to use make_unique, it's about the difference between using make_unique and using the constructor of unique_ptr directly. No, this one is not about using STL container and smart pointer together, even though you are the OP and you perceive your own problem this way. The linked question solves your problem because you are using a new expression as the argument of make_unique. Commented May 27, 2018 at 7:21
  • yes, i saw it now, thank you for clarification Commented May 27, 2018 at 7:25

1 Answer 1

1

The make_unique call is wrong: you have to forward to it the parameter you would have use to initialize Spot (none, in this case)

std::make_unique<Spot>() 

It's up to make_unique to call new.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.