-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?

Asator
  • 31
  • 8
  • 1
    `std::array` is irrelevant. You can use a plain `unique_ptr` and have the same error. – chris May 27 '18 at 07: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`. –  May 27 '18 at 07:21
  • yes, i saw it now, thank you for clarification – Asator May 27 '18 at 07:25

1 Answers1

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.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63