Copying a vector through constructor

I'm trying to copy another class' vector to cActionPuppet with the following code.
(I apologize for any formatting issues, for some reason, the site won't let me use the formatting tools)

1
2
3
4
5
6
cActionPuppet::cActionPuppet(std::vector< std::unique_ptr<ActionMove> > _cache)
	:cActor(),
	mActionMoveCache(_cache)
{

}


The general idea is that vector mActionMove copies the contents of _cache. Here's the declaration in cActionPuppet.h:

1
2
3
4
5
6
7
8
9
10
class cActionPuppet
	:public cActor
{
public:
	cActionPuppet(std::vector< std::unique_ptr<ActionMove> > _cache);

	void		updateCurrent(float dt, sf::Time _time);
	std::vector< std::unique_ptr<ActionMove> >			mActionMoveCache;

};


VS' intellisense returns no errors, but upon compiling, I get this error:

"Error 1 error C2280: 'std::unique_ptr<ActionMove,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' :
attempting to reference a deleted function C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xmemory0
"

Thanks in advance.
Last edited on
Simpler case: You want to make a copy of std::unique_ptr object. Does unique_ptr have copy constructor or copy assignment? If not, why?
I'm not trying to copy the ptr. I'm trying to copy the vector.
A vector that contains unique_ptr elements.
If I make a reference, then it would no longer copy the vector, which would go against what I'm attempting, here.
Last edited on
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <memory>
#include <vector>

struct action_move
{
    using pointer = std::unique_ptr<action_move> ;

    virtual ~action_move() = default ;

    // make a copy with dynamic storage duration and return a unique pointer to it
    virtual pointer clone() const { return std::make_unique<action_move>(*this) ; }
    // ...
};

struct move_randomly : action_move
{
    // override clone in derived classes (could use crtp if there are many derived classes)
    virtual pointer clone() const override { return std::make_unique<move_randomly>(*this) ; }
    // ...
};

struct action_puppet
{
    action_puppet( const std::vector<action_move::pointer>& cache )
    { for( const auto& ptr : cache ) action_move_cache.push_back( ptr->clone() ) ; }

    std::vector<action_move::pointer> action_move_cache ;
};
I'm still not entirely sure I understand. I don't even understand the issue here, that's what I'm trying to figure out.
> I don't even understand the issue here

Can you answer these questions?

1. Is ActionMove an object-oriented type (ie. a run-time polymorphic type, ie. a class with virtual functions)?

If the answer to 1. is yes,
2. How does one make a copy of the actual object pointed to by a std::unique_ptr<ActionMove>?
(Note that the dynamic type of the object may be a derived type of ActionMove.)

If the answer to 1. is no,
2. Why is a vector of pointers required? Why not a vector of values std::vector<ActionMove>?
ActionMove is a class that's made to log player actions so that a puppet can repeat his actions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class ActionMove :
	public cAction
{
public:
	ActionMove(sf::Time _time, 
		sf::Vector2f _direction);

	bool			Execute(sf::Time _ctime);
	void			setEndTime(sf::Time _endTime);

protected:
	cActor*			mActor;
	sf::Vector2f	mDirection;

	sf::Time		mEndTime;


};



> If the answer to 1. is yes,
2. How does one make a copy of the actual object pointed to by a std::unique_ptr<ActionMove>?
(Note that the dynamic type of the object may be a derived type of ActionMove.)

I use a function called dumpLog() for my player, where mActionMoveLog is a vector of all of his logged actions.

1
2
3
4
5
6
7
std::unique_ptr<cActionPuppet> cPlayer::dumpLog()
{
	std::unique_ptr<cActionPuppet> _puppet(new cActionPuppet(mActionMoveLog));

	return _puppet;
}


Sorry if I sound kind of angry, by the way. I've just been stuck on this for so long and I have no idea what could be wrong.
What is the definition of class cActionPuppet?
Are there any classes that derive from tt]cActionPuppet[/tt]?
You mean the constructor?

Header file:

1
2
3
4
5
6
7
8
9
10
11
class cActionPuppet
	:public cActor
{
public:
														cActionPuppet(std::vector< std::unique_ptr<ActionMove> > _cache);

	void												updateCurrent(float dt, sf::Time _time);
	std::vector< std::unique_ptr<ActionMove> >			mActionMoveCache;

};



Source file:

1
2
3
4
5
6
cActionPuppet::cActionPuppet(std::vector< std::unique_ptr<ActionMove> > _cache)
	:cActor(),
	mActionMoveCache(_cache)
{

}


No, ActionPuppet doesn't act as a parent class for anything.
std::unique_ptr<> implements sole ownership semantics. That means that there can't be two std::unique_ptr<>s pointing to the same object.

If a copy of a vector of pointers is to be made, either:

a. use the smart pointer with shared ownership semantics - std::shared_ptr<> - instead of std::unique_ptr<>, and then copy the vector of shared pointers (like in the constructor)

or b. make a copy of (clone) each ActionMove (as in http://www.cplusplus.com/forum/general/169197/#msg846576.)
Ah, now I feel stupid. Thanks so much, sorry it took me a while to respond! :)

Topic archived. No new replies allowed.