DEV Community

AbsoluteGoddus
AbsoluteGoddus

Posted on

Weird Error while working with std:unique_ptr

I'm currently developing my own game engine, C-Engine, and I've hit an unexpected roadblock while implementing scenes. Suddenly, I encountered an error that has left me stumped.

the error seems to be something along the lines of

In file included from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/vector:65,
                 from F:/Cpp/C-Engine/engine/headers.hpp:20,
                 from F:/Cpp/C-Engine/engine/GameObject.hpp:8,
                 from F:/Cpp/C-Engine/engine/engine.hpp:8,
                 from F:/Cpp/C-Engine/main.cpp:1:
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h: In instantiation of 'constexpr bool std::__check_constructible() [with _ValueType = unique_ptr<engine::GameObjectBehavior>; _Tp = const unique_ptr<engine::GameObjectBehavior>&]':
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:182:4:   required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:373:37:   required from 'constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*; _Tp = unique_ptr<engine::GameObjectBehavior>]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_vector.h:601:31:   required from 'constexpr std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<engine::GameObjectBehavior>; _Alloc = std::allocator<std::unique_ptr<engine::GameObjectBehavior> >]'
F:/Cpp/C-Engine/engine/GameObject.hpp:13:11:   required from here
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:90:56: error: static assertion failed: result type must be constructible from input type
   90 |       static_assert(is_constructible<_ValueType, _Tp>::value,
      |                                                        ^~~~~
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:90:56: note: 'std::integral_constant<bool, false>::value' evaluates to false
In file included from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_iterator.h:85,
                 from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_algobase.h:67,
                 from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/specfun.h:43,
                 from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/cmath:3716,
                 from F:/Tools/Vulkan/Include/glm/exponential.hpp:21,
                 from F:/Tools/Vulkan/Include/glm/detail/func_geometric.inl:1,
                 from F:/Tools/Vulkan/Include/glm/geometric.hpp:116,
                 from F:/Tools/Vulkan/Include/glm/detail/func_matrix.inl:1,
                 from F:/Tools/Vulkan/Include/glm/matrix.hpp:161,
                 from F:/Tools/Vulkan/Include/glm/detail/type_mat4x4.inl:1,
                 from F:/Tools/Vulkan/Include/glm/detail/type_mat4x4.hpp:188,
                 from F:/Tools/Vulkan/Include/glm/ext/matrix_double4x4.hpp:5,
                 from F:/Tools/Vulkan/Include/glm/mat4x4.hpp:5,
                 from F:/Cpp/C-Engine/engine/headers.hpp:15:
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h: In instantiation of 'constexpr void std::_Construct(_Tp*, _Args&& ...) [with _Tp = unique_ptr<engine::GameObjectBehavior>; _Args = {const unique_ptr<engine::GameObjectBehavior, default_delete<engine::GameObjectBehavior> >&}]':
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:120:21:   required from 'constexpr _ForwardIterator std::__do_uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:371:30:   required from 'constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*; _Tp = unique_ptr<engine::GameObjectBehavior>]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_vector.h:601:31:   required from 'constexpr std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<engine::GameObjectBehavior>; _Alloc = std::allocator<std::unique_ptr<engine::GameObjectBehavior> >]'
F:/Cpp/C-Engine/engine/GameObject.hpp:13:11:   required from here
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h:115:28: error: no matching function for call to 'construct_at(std::unique_ptr<engine::GameObjectBehavior>*&, const std::unique_ptr<engine::GameObjectBehavior>&)'
  115 |           std::construct_at(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h:94:5: note: candidate: 'template<class _Tp, class ... _Args> constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...)'
   94 |     construct_at(_Tp* __location, _Args&&... __args)
      |     ^~~~~~~~~~~~
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h:94:5: note:   template argument deduction/substitution failed:
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h: In substitution of 'template<class _Tp, class ... _Args> constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = std::unique_ptr<engine::GameObjectBehavior>; _Args = {const std::unique_ptr<engine::GameObjectBehavior, std::default_delete<engine::GameObjectBehavior> >&}]':
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h:115:21:   required from 'constexpr void std::_Construct(_Tp*, _Args&& ...) [with _Tp = unique_ptr<engine::GameObjectBehavior>; _Args = {const unique_ptr<engine::GameObjectBehavior, default_delete<engine::GameObjectBehavior> >&}]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:120:21:   required from 'constexpr _ForwardIterator std::__do_uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:371:30:   required from 'constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*; _Tp = unique_ptr<engine::GameObjectBehavior>]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_vector.h:601:31:   required from 'constexpr std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<engine::GameObjectBehavior>; _Alloc = std::allocator<std::unique_ptr<engine::GameObjectBehavior> >]'
F:/Cpp/C-Engine/engine/GameObject.hpp:13:11:   required from here
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h:96:17: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = engine::GameObjectBehavior; _Dp = std::default_delete<engine::GameObjectBehavior>]'
   96 |     -> decltype(::new((void*)0) _Tp(std::declval<_Args>()...))
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/std_thread.h:43,
                 from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/stop_token:37,
                 from C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/thread:42,
                 from F:/Cpp/C-Engine/engine/headers.hpp:22:
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/unique_ptr.h:522:7: note: declared here
  522 |       unique_ptr(const unique_ptr&) = delete;
      |       ^~~~~~~~~~
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h: In instantiation of 'constexpr void std::_Construct(_Tp*, _Args&& ...) [with _Tp = unique_ptr<engine::GameObjectBehavior>; _Args = {const unique_ptr<engine::GameObjectBehavior, default_delete<engine::GameObjectBehavior> >&}]':
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:120:21:   required from 'constexpr _ForwardIterator std::__do_uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_uninitialized.h:371:30:   required from 'constexpr _ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const unique_ptr<engine::GameObjectBehavior>*, vector<unique_ptr<engine::GameObjectBehavior> > >; _ForwardIterator = unique_ptr<engine::GameObjectBehavior>*; _Tp = unique_ptr<engine::GameObjectBehavior>]'
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_vector.h:601:31:   required from 'constexpr std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<engine::GameObjectBehavior>; _Alloc = std::allocator<std::unique_ptr<engine::GameObjectBehavior> >]'
F:/Cpp/C-Engine/engine/GameObject.hpp:13:11:   required from here
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/stl_construct.h:119:7: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = engine::GameObjectBehavior; _Dp = std::default_delete<engine::GameObjectBehavior>]'
  119 |       ::new((void*)__p) _Tp(std::forward<_Args>(__args)...);
      |       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C:/Users/absol/AppData/Local/Programs/CLion/bin/mingw/lib/gcc/x86_64-w64-mingw32/13.1.0/include/c++/bits/unique_ptr.h:522:7: note: declared here
  522 |       unique_ptr(const unique_ptr&) = delete;
      |       ^~~~~~~~~~
Enter fullscreen mode Exit fullscreen mode

The Complete error is on github if you wan't to look at it. (https://github.com/AbsoluteGoddus/C-Engine/blob/main/error.txt)

I've tried to seperate the error but can't seem to fin the source but im suspecting that the error is somere in here

//
// Created by absol on 5/20/2024.
//

#ifndef C_ENGINE_SCENE_HPP
#define C_ENGINE_SCENE_HPP

#include "headers.hpp"
#include "GameObject.hpp"

namespace engine {
    class Scene {
    private:
        std::vector<GameObject> gameObjects;

    public:
        std::string name;
        long int ID;

        void pushObject(GameObject object) {
            gameObjects.push_back(std::move(object));
        }

        void init() {
            for (auto &object : gameObjects) {
                object.init();
            }
        }

        void update() {
            for (auto &object : gameObjects) {
                object.preUpdate();
            }

            for (auto &object : gameObjects) {
                object.update();
            }

            for (auto &object : gameObjects) {
                object.postUpdate();
            }
        }

        GameObject* getObjectById(long int ID) {
            for (auto &object : gameObjects) {
                if (object.ID == ID) {
                    return &object;
                }
            }
            return nullptr;
        }

        GameObject* getObjectByName(std::string name) {
            for (auto &object: gameObjects) {
                if (object.name == name) {
                    return &object;
                }
            }
            return nullptr;
        }

        void destroy() {
            for (auto &object : gameObjects) {
                object.destroy();
            }
        }

        void terminate() {
            for (auto &object : gameObjects) {
                object.terminate();
            }
            gameObjects.clear();
        }
    };
}

#endif //C_ENGINE_SCENE_HPP

Enter fullscreen mode Exit fullscreen mode

or here

//
// Created by absol on 5/18/2024.
//

#ifndef C_ENGINE_GAMEOBJECT_HPP
#define C_ENGINE_GAMEOBJECT_HPP

#include "headers.hpp"
#include "GameObjectBehavior.hpp"
#include "util.hpp"

namespace engine {
    class GameObject {

    public:
        GameObject() {};

        long int ID;
        std::string name;
        std::vector<Behavior> Behaviors;

        void init() {
            for (auto &behavior : Behaviors) {
                behavior->init();
            }
        }

        void preUpdate() {
            for (auto &behavior : Behaviors) {
                behavior->preUpdate();
            }
        }

        void update() {
            for (auto &behavior : Behaviors) {
                behavior->update();
            }
        }

        void postUpdate() {
            for (auto &behavior : Behaviors) {
                behavior->postUpdate();
            }
        }

        void destroy() {
            for (auto &behavior : Behaviors) {
                behavior->destroy();
            }
        }

        void terminate() {
            for (auto &behavior : Behaviors) {
                behavior->terminate();
            }
        }
    };
}

#endif //C_ENGINE_GAMEOBJECT_HPP

Enter fullscreen mode Exit fullscreen mode

or here

#ifndef C_ENGINE_GAMEOBJECTBEHAVIOR_HPP
#define C_ENGINE_GAMEOBJECTBEHAVIOR_HPP

namespace engine {
    class GameObjectBehavior {
    public:
        virtual void init() {
            // Definition for init
        }

        virtual void preUpdate() {
            // Definition for preUpdate
        }

        virtual void update() {
            // Definition for update
        }

        virtual void postUpdate() {
            // Definition for postUpdate
        }

        virtual void destroy() {
            // Definition for destroy
        }

        virtual void terminate() {
            // Definition for termination
        }
    };
}

#endif //C_ENGINE_GAMEOBJECTBEHAVIOR_HPP
Enter fullscreen mode Exit fullscreen mode

Util.hpp is

using Behavior = std::unique_ptr<engine::GameObjectBehavior>;

template <typename T>
Behavior make_behavior() {
    return std::make_unique<T>();
}

Enter fullscreen mode Exit fullscreen mode

(Sorry for bad Grammar)

Top comments (4)

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Why are you trying to construct an arbitrary T when you want a Behavior? And the unique_ptr should not be hidden behind the type. It should be:

using Behavior = engine::GameObjectBehavior;

template<typename... Args>
inline std::unique_ptr<Behavior> make_behavior( Args&&... args ) {
    return std::make_unique<Behavior>( std::forward<Args>( args )... );
}
Enter fullscreen mode Exit fullscreen mode

As an aside, I don't think you should have make_behavior at all: just call the std::make_unique<Behavior>() directly.

Collapse
 
pauljlucas profile image
Paul J. Lucas

I corrected my code.

Collapse
 
absolutegoddus profile image
AbsoluteGoddus

I used make_behavior to make the whole thing more understandable to someone who just wan't to develop a game in that engine

Collapse
 
pauljlucas profile image
Paul J. Lucas

IMHO, you're doing them a disservice. Instead, they should actually learn C++.