Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1224

Like
0Likes
Like

Posted 13 April 2014 - 02:01 PM

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>          ]

Edited by BaneTrapper, 13 April 2014 - 02:02 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9623

Like
4Likes
Like

Posted 13 April 2014 - 02:28 PM

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.



#3 BaneTrapper   Members   -  Reputation: 1224

Like
0Likes
Like

Posted 13 April 2014 - 02:53 PM

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!


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS