正在尝试引用已删除的函数 shared_ptr

Attempting to reference Deleted function shared_ptr

我有以下代码

inline entity_ptr Parser::parse(const std::string& json_str)
{
    // std::cout << "parse: " << json_str << "\n";
    entity_ptr entity = do_parse(json_str);
    if (entity && entity->is_notification())
    {
        notification_ptr notification = std::dynamic_pointer_cast<jsonrpcpp::Notification>(entity);
        if (notification_callbacks_.find(notification->method()) != notification_callbacks_.end())
        {
            notification_callback callback = notification_callbacks_[notification->method()];
            if (callback)
                callback(notification->params());
        }
    }
    else if (entity && entity->is_request())
    {
        request_ptr request = std::dynamic_pointer_cast<jsonrpcpp::Request>(entity);
        if (request_callbacks_.find(request->method()) != request_callbacks_.end())
        {
            request_callback callback = request_callbacks_[request->method()];
            if (callback)
            {
                jsonrpcpp::response_ptr response = callback(request->id(), request->params());
                if (response)
                    return response;
            }
        }
    }
    return entity;
}

编译时我收到以下错误

error C2280: 'std::shared_ptrjsonrpcpp::Notification std::dynamic_pointer_castjsonrpcpp::Notification,jsonrpcpp::Entity(const std::shared_ptrjsonrpcpp::Entity &) noexcept': attempting to reference a deleted function

通知class如下

class Notification : public Entity
{
public:
    Notification(const Json& json = nullptr);
    Notification(const char* method, const Parameter& params = nullptr);
    Notification(const std::string& method, const Parameter& params);

    Notification(const Notification&) = default;
    Notification(Notification &&) = default;
    Notification& operator=(const Notification&) = default;

    Json to_json() const override;
    void parse_json(const Json& json) override;

    const std::string& method() const
    {
        return method_;
    }

    const Parameter& params() const
    {
        return params_;
    }

protected:
    std::string method_;
    Parameter params_;
};

据我所知,错误是基于这样一个事实,即我的 move/param 构造在我声明它们时并未明确实现,但仍然收到错误,知道如何修复它吗?

基础class如下

class Entity
{
public:

    Entity(entity_t type);
    virtual ~Entity() = default;
    Entity(const Entity&) = default;
    Entity& operator=(const Entity&) = default;
    Entity(Entity&&) = default;

    virtual std::string type_str() const;

    virtual Json to_json() const = 0;
    virtual void parse_json(const Json& json) = 0;

    virtual void parse(const std::string& json_str);
    virtual void parse(const char* json_str);

protected:
    entity_t entity;
};

如有任何帮助,我们将不胜感激

感谢

显然“定义不当的成员变量”会导致默认函数被删除。 (有点奇怪)

尝试改变您定义此方法的方式:

virtual Json to_json() const = 0;