0

We are supposed to declare the private member variable trackerCoordinates as an atomic. The .h was originally written like:

class Missile : public Attack, public Sea::Coordinates {
    private:
        bool hit = false;
        
        Sea::FCoordinates trackerCoordinates;

I've tried to use the std::atomic like this:

class Missile : public Attack, public Sea::Coordinates {
    private:
        bool hit = false;
        
        std::atomic<Sea::FCoordinates> trackerCoordinates;

Now, in the accompanying .cpp, in line 3, there's a new error, which gets resolved by undoing the std::atomic declaration.

Missile::Missile(Coordinates const & coordinates)
    : Coordinates(coordinates),
    trackerCoordinates(-1.f,-1.f) -- this line
    {
    }

The error says:

No matching constructor for initialization of 'std::atomicSea::FCoordinates' (aka 'atomic<TCoordinates>')

I do not know why that error happens and how to resolve it. A bit more information: FCoordinates is a typedef of TCoordinates, which is supposed to create coordinates given as a parameter like (x,y), and the TCoordinates constructor is noexcept declared.

Thanks in Advance for any help.

  • Unrelated: Why is `Missile` inheriting from `Coordinates`? A missile isn't a coordinate, is it? – Ted Lyngmo Jul 05 '21 at 14:14
  • 2
    The [constructor of `std::atomic`](https://en.cppreference.com/w/cpp/atomic/atomic/atomic) does not forward its arguments to the constructor of the element type. It expects a single fully-built element instead. E.g. `trackerCoordinates(Sea::FCoordinates(-1.f,-1.f))` – dyp Jul 05 '21 at 14:16
  • 1
    Also, this should pass: `static_assert(std::is_trivially_copyable_v && std::is_copy_constructible_v && std::is_move_constructible_v && std::is_copy_assignable_v && std::is_move_assignable_v, "");` – Ted Lyngmo Jul 05 '21 at 14:21
  • Not a duplicate, but closest I've found so far is [Custom type in std::atomic](https://stackoverflow.com/a/52669095) which does show calling the `T` constructor to get a value to pass to the `atomic` constructor. – Peter Cordes Jul 05 '21 at 21:25

0 Answers0