Error from/with unique_ptr trying to acces private member, error C2248

Started by
1 comment, last by BaneTrapper 10 years ago

Hello, i was doing work on my game and i stumbled upon a problem i cant resolve.

I want to be able to assign NewUnit = OldUnit

Commenting one line makes the error go away, thus i am suspecting that is the issue, but i do not know how to resolve.


class Item

{

public:

    Item();

    virtual void AddToInventory();

    virtual void AddToTown();

    virtual void PrintData();

};
class Bag

{

public:

    Bag();

    int bagMaxSize;

    std::vector<std::unique_ptr<Item>> itemList;

};



class InventoryForUnits

{

public:

    InventoryForUnits();

    //void AddBag(Bag & BagTMP);

    //void AddItemToBag(int bagID, Item & item);

    //void PrintData();



    int bagMaxAmount;

    std::vector<Bag> bagList;//Comenting this line makes the error not appear

};
//Somewhere in Unit.h
class Unit
{
public:
InventoryForUnits inventory;
}
//Somewhere in unit.cpp
int UnitList::AddUnit(Unit & objUnit)//Returns index of unit added
{
//Check if can pick from empty pos in vector
    if(emptyUnitListPos.size() > 0)
    {
        const int id = emptyUnitListPos.size()-1;
        unitList[id] = objUnit;
        unitList[id].isInUse = true;//This is set with the new object passed in anyway
        objEntity.AddUnitEntity(unitList[id].entitySetting);
        objEntity.MoveUnit(unitList[id].entitySetting, sf::Vector2f(unitList[id].unitProperties.posX, unitList[id].unitProperties.posY));
        emptyUnitListPos.pop_back();

        return id;
    }
//Add new position to vector
    else
    {
        unitList.push_back(objUnit);
        const int id = unitList.size()-1;
        unitList[id].isInUse = true;//This is set with the new object passed in anyway
        objEntity.AddUnitEntity(unitList[id].entitySetting);
        objEntity.MoveUnit(unitList[id].entitySetting, sf::Vector2f(unitList[id].unitProperties.posX, unitList[id].unitProperties.posY));
        return id;
    }
    return -1;
}

The error when compiling


1>C:\Program Files\Microsoft Visual Studio 11.0\VC\include\xmemory0(606): error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'

1>          with

1>          [

1>              _Ty=Item

1>          ]

1>          C:\Program Files\Microsoft Visual Studio 11.0\VC\include\memory(1447) : see declaration of 'std::unique_ptr<_Ty>::unique_ptr'

1>          with

1>          [

1>              _Ty=Item

1>          ]

1>          C:\Program Files\Microsoft Visual Studio 11.0\VC\include\xmemory0(605) : while compiling class template member function 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)'

1>          with

1>          [

1>              _Ty=std::unique_ptr<Item>

1>          ]

1>          C:\Program Files\Microsoft Visual Studio 11.0\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct(_Ty *,const _Ty &)' being compiled

1>          with

1>          [

1>              _Ty=std::unique_ptr<Item>

1>          ]

1>          C:\Program Files\Microsoft Visual Studio 11.0\VC\include\type_traits(743) : see reference to class template instantiation 'std::allocator<_Ty>' being compiled

1>          with

1>          [

1>              _Ty=std::unique_ptr<Item>

1>          ]

1>          C:\Program Files\Microsoft Visual Studio 11.0\VC\include\vector(655) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled

1>          with

1>          [

1>              _Ty=std::allocator<std::unique_ptr<Item>>

1>          ]

1>          c:\c++\therpg\therpg\Inventory.h(53) : see reference to class template instantiation 'std::vector<_Ty>' being compiled

1>          with

1>          [

1>              _Ty=std::unique_ptr<Item>

1>          ]
Advertisement

A unique_ptr is movable but not copyable, which means a std::vector of unique_ptrs is also not copyable. And a class that contains a std::vector of unique_ptrs would also not be copyable with the default copy constructor or assignment operator. One option in your case is to give your Bag a copy constructor that clones all the pointed to Items. Another option is to use shared_ptrs instead of unique_ptrs.

A unique_ptr is movable but not copyable, which means a std::vector of unique_ptrs is also not copyable. And a class that contains a std::vector of unique_ptrs would also not be copyable with the default copy constructor or assignment operator. One option in your case is to give your Bag a copy constructor that clones all the pointed to Items. Another option is to use shared_ptrs instead of unique_ptrs.

Ive bean doing all sort of crazy operator overloading trying to get it to work, got a clear head now i fixed the issue.

Thx SiCrane!

This topic is closed to new replies.

Advertisement