【问题标题】:Attempting to reference Deleted function shared_ptr试图引用已删除的函数 shared_ptr
【发布时间】:2021-02-17 18:09:53
【问题描述】:

我有以下代码

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

任何帮助将不胜感激

谢谢

【问题讨论】:

    标签: c++ deleted-functions


    【解决方案1】:

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

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

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

    virtual Json to_json() const = 0;

    【讨论】:

    • 感谢您的回复,但实际上它并没有改变任何东西,我用一个简单的'virtual Json to_json();'替换了变量但一直收到同样的错误,希望有另一种方法来解决这个问题:(谢谢
    • 实际上将动态更改为静态解决了问题
    猜你喜欢
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多