简体   繁体   English

Unique_ptr模板错误

[英]Unique_ptr template error

I have the following class with a vector of unique_ptrs: 我有一个带有unique_ptrs向量的以下类:

class StatManager
{
private:
    std::vector<std::unique_ptr<IStat>> stats;
public:

    void setStat(const Stat<bool>& stat) {
        stats.emplace_back(stat.clone());
    }

    void setStat(const Stat<float>& stat) {
        stats.emplace_back(stat.clone());
    }

    void setStat(const Stat<int>& stat) {
        stats.emplace_back(stat.clone());
    }

    bool getStatValue(std::string name, float &value) {
        for (unsigned int x = 0; x < stats.size(); x++)
        {
            if (stats.at(x)->getName() == name) {
                value = stats.at(x)->getAsFloat();
                return true;
            }
        }
        return false;
    }

    bool getStatValue(std::string name, int &value) {
        for (unsigned int x = 0; x < stats.size(); x++)
        {
            if (stats.at(x)->getName() == name) {
                value = stats.at(x)->getAsInt();
                return true;
            }
        }
        return false;
    }

    bool getStatValue(std::string name, bool &value) {
        for (unsigned int x = 0; x < stats.size(); x++)
        {
            if (stats.at(x)->getName() == name) {
                value = stats.at(x)->getAsBool();
                return true;
            }
        }
        return false;
    }
};

And the class that implements it: 和实现它的类:

class BaseItem
{
private:
    int id;
    std::string name;
public:
    BaseItem(int itemId, std::string itemName) : id(itemId), name(itemName) {}

    virtual int getID() = 0;
    virtual std::string getName() = 0;
};


class Item : public BaseItem
{
private:
    StatManager statManager; //ERROR HERE trying to implement StatManager
public:
    Item(int id, std::string name);

    virtual int getID();
    virtual std::string getName();
};

I get the following error on visual studio 2013 while trying to make an attribute of StatManager: 尝试制作StatManager的属性时,在Visual Studio 2013上出现以下错误:

Error   5   error C2280: 'std::unique_ptr<IStat,std::default_delete<_Ty>>::
  unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)'
  : \xmemory0   593 1   GameTheory++

So in my main class i just try to create a local instance of StatManager and it work without any problem, for example: 因此,在我的主类中,我只是尝试创建StatManager的本地实例,它可以正常工作,例如:

float f = 0; 
StatManager manager; //Works flawless
manager.setStat(ItemStat("Durabilit", 100, ""));
manager.getStatValue("Durabilit", f);

So the problem happens only when trying to make StatManager as an attribute of any class... Is there anything i am missing, probably some move problem with the unique ptrs? 因此,仅当尝试将StatManager设置为任何类的属性时,问题才会发生...是否缺少我所缺少的内容,可能是唯一ptrs的某些移动问题? I really dont know, thanks in advance. 我真的不知道,在此先感谢。

EDITED: 编辑:

Only place where i am using Item class is in the main method and trying to make a local instance of it: 我使用Item类的唯一地方是在main方法中,并试图使其成为本地实例:

Item item = Item(0, "Test");

It's not making StatManager an attribute of a class that's failing, it's trying to copy it. 它不是使StatManager成为失败类的属性,而是试图复制它。 Since it contains a vector of unique_ptrs, the whole class is non-copyable. 由于它包含一个unique_ptrs向量,因此整个类是不可复制的。 Presumably you're trying to copy it in some code you haven't posted. 大概是您试图将其复制为尚未发布的某些代码。

To better diagnose the source of this problem, add this to your Item class: 为了更好地诊断此问题的根源,请将其添加到您的Item类中:

Item(const Item&) = delete;

That way you'll get an error straight away when trying to copy it, rather than an error about unique_ptr buried within Item. 这样,您在尝试复制它时会立即得到一个错误,而不是关于埋在Item中的unique_ptr错误。

Edit: OK, so the code you posted just now is where the problem is: 编辑:确定,所以您刚才发布的代码就是问题所在:

Item item = Item(0, "Test");

This means "create an Item and copy it to item . Instead, do this: 这意味着“创建一个项目并将其复制到item 。相反,请执行以下操作:

Item item(0, "Test");

This just creates an Item, and should work. 这只会创建一个项目,并且应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM