I have the following class with a vector of 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:
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:
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? 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 item = Item(0, "Test");
It's not making StatManager an attribute of a class that's failing, it's trying to copy it. Since it contains a vector of unique_ptrs, the whole class is non-copyable. 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(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.
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(0, "Test");
This just creates an Item, and should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.