试图引用已删除的函数 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;
}

编译时出现以下错误

错误 C2280:'std::shared_ptrjsonrpcpp::Notification std::dynamic_pointer_castjsonrpcpp::Notification,jsonrpcpp::Entity(const std::shared_ptrjsonrpcpp::Entity &) noexcept': 试图 引用已删除的函数

通知类如下

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 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;
};

任何帮助将不胜感激

谢谢

loulizhi969 回答:试图引用已删除的函数 shared_ptr

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

https://stackoverflow.com/a/37517125/2934222

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

virtual Json to_json() const = 0;

本文链接:https://www.f2er.com/906994.html

大家都在问