C2280

Dec 21, 2015 at 10:51pm
Greetings! this declaration:
 
std::vector<std::unique_ptr<Piece>> formations;

Is giving me a C2280 (attempting to reference a deleted function)
1
2
3
4
5
6
virtual ~Piece();

Piece::~Piece()
{

}

After a fun game of hunting down and disabling all unique_ptr s this was the one found to be the culprit. Any insight is appreciated
Dec 21, 2015 at 11:07pm
Probably because Piece is missing a default constructor. Try adding

Peace::Peace() {}

At the top of your Piece class.

Edit: Also please copy paste the entire error, not sure why you would just type C2280.
Last edited on Dec 21, 2015 at 11:10pm
Dec 21, 2015 at 11:13pm
When referencing a compilation error, please post the full text of the error message.

When supplying code, please provide a minimal, compilable snippet that reproduces the problem you are experiencing.

going by what you say the following should reproduce the problem (but doesn't.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <memory>
#include <vector>

struct Piece
{
    virtual ~Piece();
};

Piece::~Piece()
{
}

int main()
{
    std::vector<std::unique_ptr<Piece>> formations;
}


http://ideone.com/nnAigf
Dec 21, 2015 at 11:14pm
1
2
3
4
5
6
7
8
9
10
11
Piece::Piece() // not really used
	{
		xpos = 0;
		ypos = 0;
		actions = 3;
		xvel = 0;
		ypos = 0;
		xCoor = 0;
		yCoor = 0;
	}
	

I tried without a body too, still there

Error 9 error C2280: 'std::unique_ptr<Piece,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 593 1 P2
Dec 21, 2015 at 11:20pm
Error 9


This is the 9th error encountered during compilation? What is the first? Again, supplying a minimal compilable snippet of code that produces the same issue would speed things along.

Well, not compilable. But one that produces the same error.
Last edited on Dec 21, 2015 at 11:22pm
Dec 21, 2015 at 11:27pm
The rest are warnings, signed/unsigned mismatch

Again, supplying a minimal compilable snippet of code that produces the same issue would speed things along.

Working on it
Dec 21, 2015 at 11:38pm
Hi,

Are you sure that it is that declaration that is giving trouble, not the use of it?

Anything move constructable has it's copy ctor and assignment deleted, after all it's a unique pointer. Are you trying to copy something into the vector? Could you try to std::move it instead?

Or could you make it a vector of shared_ptr ?

Dec 22, 2015 at 1:42am
I'm not able to reproduce the error...
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef A_H
#define A_H
class A
{
public:
	A();
	A(int);
	virtual ~A();
	int x;
};
#endif

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "A.h"
A::A()
{

}
A::A(int X)
{
	x = X;
}
A::~A()
{

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef B_H
#define B_H
#include "A.h"
#include <memory>
#include <vector>
class B :  A
{
public:
	B();
	B(int);
	~B();
	std::unique_ptr<A> a;
	};
#endif 

1
2
3
4
5
6
7
8
9
#include "B.h"
B::B(int xToA)
{
	a = std::make_unique<A>(xToA);
}
B::~B()
{

}

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef C_H
#define C_H
#include "B.h"
#include <vector>
class C
{
public:
	C();
	C(int);
	std::vector < std::unique_ptr<A> > k;
	void add(std::unique_ptr<A>);
};
#endif; 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "C.h"

C::C()
{

}
C::C(int c)
{

}
void::C::add(std::unique_ptr<A> a)
{
	k.push_back(std::move(a));
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include "B.h"
#include "C.h"


int main(int argc, char* args[])
{
	bool run = true;
	B b(10);
	C c(2);
	c.add(std::move(b.a));
	while (run)
	{

	}
}

It compiles and runs.
Dec 22, 2015 at 1:55am
@theIdeasMan
 
//	std::vector<std::unique_ptr<Piece>> formations; 

The program '[4704] P2.exe' has exited with code 0 (0x0).
 
std::vector<std::unique_ptr<Piece>> formations;

Error 1 error C2280: 'std::unique_ptr<Piece,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 593 1 P2
It convinced me. I disabled everything and enabled/build until I found one that broke, then proceeded until it happened again, that was the only one that broke.

Are you trying to copy something into the vector? Could you try to std::move it instead?
 
k.push_back(std::move(a));
?
May have to consider your suggestion for shared_ptr tho. Thanks
Dec 22, 2015 at 2:07am
Don't use shared_ptr unless you need shared ownership.
The problem is you are copying the unique_ptr
1
2
3
4
void::C::add(std::unique_ptr<A> a)
{
	k.push_back(std::move(a));
}


Sorry, I'm wrong. You std::move it when calling c.add.
Last edited on Dec 22, 2015 at 2:11am
Dec 22, 2015 at 2:32am
By default it makes a copy but with the std::move it's not copying, it's putting it in there no copy
 
k.push_back(a); // C2280 

1
2
3
4
void::C::add(std::unique_ptr<A> a)
{
	k.push_back(std::move(a));
}


It compiles and runs.

My problem is with the similar, but working, declaration
1
2
std::vector < std::unique_ptr<A> > k;

What would break a declaration?
No worries my friend!
Last edited on Dec 22, 2015 at 2:54am
Dec 22, 2015 at 3:54am
Is there ever a copy being made of the objects that contain the vector of unique_ptr?
Dec 22, 2015 at 4:45am
Sort of, I rewrote that object and recompiled piece by piece, this is where is started breaking again:

1
2
3
Faction2 player(false);
std::vector<Faction2> factions;
factions.push_back(std::move(player)); // breaks it 

I take it that if an object has a unique_ptr data member and you send a copy of that object somewhere it violates the Highlander rule of there can only be one?

Last edited on Dec 22, 2015 at 5:02am
Dec 22, 2015 at 7:50am
unique_ptr has deleted copy ctor and operator= so copying an object that contains a unique_ptr will attempt to copy the unique_ptr. The code snippet you posted shouldn't be an issue since you move player.
Topic archived. No new replies allowed.