Error: Use of Deleted Function

error: use of deleted function

The error message clearly says that the default constructor has been deleted implicitly. It even says why: the class contains a non-static, const variable, which would not be initialized by the default ctor.

class X {
const int x;
};

Since X::x is const, it must be initialized -- but a default ctor wouldn't normally initialize it (because it's a POD type). Therefore, to get a default ctor, you need to define one yourself (and it must initialize x). You can get the same kind of situation with a member that's a reference:

class X { 
whatever &x;
};

It's probably worth noting that both of these will also disable implicit creation of an assignment operator as well, for essentially the same reason. The implicit assignment operator normally does members-wise assignment, but with a const member or reference member, it can't do that because the member can't be assigned. To make assignment work, you need to write your own assignment operator.

This is why a const member should typically be static -- when you do an assignment, you can't assign the const member anyway. In a typical case all your instances are going to have the same value so they might as well share access to a single variable instead of having lots of copies of a variable that will all have the same value.

It is possible, of course, to create instances with different values though -- you (for example) pass a value when you create the object, so two different objects can have two different values. If, however, you try to do something like swapping them, the const member will retain its original value instead of being swapped.

How to take care of error: use of deleted function c++?

Ok, so this is a fun one. Have a look here:

template <class T>
class LinkedList {
struct Node {
T data; // Here you create a T

When you create a T in node, how do you do it? Well you have to call its constructor. Now lets look at your Dummy. It doesn't have a default constructor, you must call it with Dummy(int). So therefore in Node your T which is now Dummy must be constructed with this constructor. But how!?

That is the conundrum presented by you to your compiler. The compiler has answered, in a strange but sensible way. I can't construct this Node because the default constructor is deleted. It is deleted because its T (Dummy) cannot be default constructed.

A potential fix for this is to add a new constructor to Node, one that takes and copies a T, aka:

struct Node {
Node(const T &in) : data(in) {}
T data;

You should experiment with this a little.


A little side note. You have an issue. Think about how you add subsequent nodes,, vs when you construct the first one. Your constructor LinkedList(T value) { has undefined behaviour because it dereferences a null pointer.

gcc: invalid error use of deleted function (copy constructor)?

Before C++17 such copy elision is an optimization, which is permitted but the copy (or move) constructor still must be present and accessible.

Since C++17 the code works fine because for mandatory copy elision the copy/move constructors need not be present or accessible again.

C++17 core language specification of prvalues and temporaries is fundamentally different from that of the earlier C++ revisions: there is no longer a temporary to copy/move from.

C++ Use of deleted function error

Your struct here:

struct weighted_pointer{
mutable int weight;
unique_ptr<likeatree> ptr;
};

contains a std::unique_ptr. A std::unique_ptr cannot be copied, so your entire weighted_pointer cannot be copied as well.

There are three places in your code where you attempt to copy it, which causes the errors you see:

bool operator()(const weighted_pointer lhs, const weighted_pointer rhs) {

Must be:

bool operator()(weighted_pointer const& lhs, weighted_pointer const& rhs) {
stdSet.insert(tmp);

This could theoretically be fixed by:

stdSet.insert(std::move(tmp));

However, you then cannot use tmp anymore, which you do, not only in the same loop but also in the loop below. So you must find a different solution altogether. Perhaps use emplace. Or restructure your code entirely.

auto it = find_if(stdSet.begin(),stdSet.end(),[&](weighted_pointer temp){ return temp.ptr.get() == treeVector[i]; });

Must be:

auto it = find_if(stdSet.begin(),stdSet.end(),[&](weighted_pointer const& temp){ return temp.ptr.get() == treeVector[i]; });

For VC++ 2013, the std::move fix will not suffice. You will have to add an explicit move constructor to your struct:

struct weighted_pointer{
mutable int weight;
unique_ptr<likeatree> ptr;

weighted_pointer() = default;
weighted_pointer(weighted_pointer&& src) :
weight(std::move(src.weight)),
ptr(std::move(src.ptr))
{
}
};

VC++ 2015 fixes this problem. More information: Default Move Constructor in Visual Studio 2013 (Update 3)

error: use of deleted function ‘Node::~Node()’

You have a union with a member that has a non-trivial destructor. So the union can't know how to destruct itself. That's why NodeType::~NodeType is deleted by default. That's what the message tells you.

You need to define the destructor yourself and properly call the destructor on the correct member.

Or, better yet, don't use unions at all. They are unsafe. Use std::variant which is the standard library type for "tagged unions", or type-safe unions.

C++ compiler error: use of deleted function std::variant()

While you do need to work on a minimal example, the core problem is your DraftState default constructor is ambiguous with your string constructor with a default argument. See https://godbolt.org/z/hTnsjoWaW

To be default constructible, std::variant requires the first type argument to be default constructible. The ambiguity causes the compiler to think your class is not default constructible, and therefore neither is the variant.

Also, your move constructor for Document should use the member initializer list, rather than assignment. And your DraftState is missing the copy assignment operator, though unless there's more to it, I wouldn't explicitly define all of the copy/move/destructor values. See the Rule of Five.

error: use of deleted function 'ClassName::ClassName(const ClassName&)'

The problem is that your class is not copy-able because it contains a std::stringstream (which is itself not copy-able). This results in its copy constructor being deleted, which is what the compiler is trying to tell you. To fix this, simply don't use the copy-initialization syntax in your main function.

int main() {
MyClass newClass;
}


Related Topics



Leave a reply



Submit