Error C2280: Attempting to reference a remote function (unique_ptr)

I looked at some old code that used raw pointers and replaced them with unique_ptr . Now when I try to compile the code, I get this error message:

Error 1 error C2280: 'std :: unique_ptr> :: unique_ptr (const std :: unique_ptr <_Ty, std :: default_delete <_Ty → ​​&)': attempt to reference the remote function d: \ visual studio 2013 \ VC \ enable \ xmemory0

The compiler's conclusion about the situation is enormous - to save space on this issue, see here .

As far as I can tell, this has something to do with how I use unique pointers. It starts from here (level.h, lines 65-66):

 typedef std::unique_ptr<Enemy> PEnemy; std::list<PEnemy> m_enemies; 

Now the next key that I get in the compiler output is line 47 in the source.cpp database:

 std::list<PEnemy> enemies = Game::LEVEL->getEnemies(); 

Why is this causing problems? How can I fix the error?

+7
c ++ c ++ 11
source share
1 answer

unique_ptr cannot be copied; just moved! But since std::list should be able to move them inside, the only problem should be the assignment you perform in the list itself.

Can you move the list?

  • std::list<PEnemy> enemies = std::move(Game::LEVEL->getEnemies());

Or use the link to it instead?

  • const std::list<PEnemy>& enemies = Game::LEVEL->getEnemies();

If not (and this will depend on the type of the returned value of Game::LEVEL->getEnemies() , regarding which, if you can use either of the above solutions), you will need to make a deep deep copy or switch to shared_ptr .

All this may seem like a hindrance, but actually does you a favor, strictly observing the rules of ownership of your pointers.

+15
source share

All Articles