【问题标题】:How to take care of error: use of deleted function c++?如何处理错误:使用已删除的函数 c++?
【发布时间】:2021-08-25 09:42:54
【问题描述】:

这是我的代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include "dummy.h"
using namespace std;

#ifndef SORT_H
#define SORT_H

template <class T>
class LinkedList {
    struct Node {
        T data;
        Node * next;
    };

    Node * head;

public:
    LinkedList() {
        head = NULL;
    }
    LinkedList(T value) {
        head -> data = value;
        head -> next = NULL;
    }

    ~LinkedList() {
        while(head != NULL) {
            Node * n = head->next;
            delete head;
            head = n;
        }
    }

    void add(T value) {
        Node * n = new Node;  // the error starts here probably
        n->data = value;
        n->next = head;
        head = n;
    }

    void print() {
        Node *curr = head;
        while (curr) {
            cout << curr->data << endl;
            curr = curr->next;
        }
    }
};

#endif

当我尝试编译它时,我得到了错误:使用已删除的函数'LinkedList::Node::Node()'

我知道这可能是因为我事先无法知道在创建排序列表时是否会得到任何参数,我该怎么做? 让它编译运行?

Dummy 类是这样的:

#include <iostream>
#include <cstring>
#include <string>
#include <cmath>

using namespace std;

#ifndef DUMB_H
#define DUMB_H

class Dummy {
    int num_of_teeth;
public:
    Dummy(int num) {
        num_of_teeth = num;
    }
    ~Dummy() {};
    void add(int num) {
        num_of_teeth += num;
    }
    void remove() {
        num_of_teeth --;
    }
    void print() {
        int num = num_of_teeth;
        while (num > 0) {
            cout << "D";
            if ((num-1) == (num_of_teeth/2)) {
                cout << "\n";
            }
            num --;
            if (num == 0) {
                cout << "\n";
            }
        }
    }
    friend ostream& operator << (ostream& output, Dummy& dumb)
    {
        output << dumb.num_of_teeth;
        return output;
    }
};

#endif

我的主要是:

#include <iostream>
#include "sortedList.h"
#include "dummy.h"

int main() {
    std::cout << "Hello, World!" << std::endl;

    Dummy teeth(24);
    teeth.add(7);
    Dummy slime(11);
    slime.add(1);
    Dummy josh(32);
    LinkedList<Dummy> teeth_list;
    teeth_list.add(teeth);  // The first time I try to put teeth into the list
    teeth_list.add(slime);
    teeth_list.add(josh);
    teeth_list.print();

    LinkedList <string> myList;
    myList.add("yossi");
    myList.add("Rikki");
    myList.add("Pavel <3");
    myList.print();

    return 0;
}

构建日志:

====================[ Build | exe_name | Debug ]================================
"C:\Program Files\JetBrains\CLion 2021.1.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProjects\ex2.2\cmake-build-debug --target exe_name -- -j 3
Scanning dependencies of target exe_name
[ 33%] Building CXX object CMakeFiles/exe_name.dir/main.cpp.obj
[ 66%] Building CXX object CMakeFiles/exe_name.dir/sortedList.cpp.obj
In file included from C:\Users\User\CLionProjects\ex2.2\main.cpp:2:
C:\Users\User\CLionProjects\ex2.2\sortedList.h: In instantiation of 'void LinkedList<T>::add(T) [with T = Dummy]':
C:\Users\User\CLionProjects\ex2.2\main.cpp:14:25:   required from here
C:\Users\User\CLionProjects\ex2.2\sortedList.h:38:20: error: use of deleted function 'LinkedList<Dummy>::Node::Node()'
         Node * n = new Node;
                    ^~~~~~~~
C:\Users\User\CLionProjects\ex2.2\sortedList.h:13:12: note: 'LinkedList<Dummy>::Node::Node()' is implicitly deleted because the default definition would be ill-formed:
     struct Node {
            ^~~~
C:\Users\User\CLionProjects\ex2.2\sortedList.h:13:12: error: no matching function for call to 'Dummy::Dummy()'
In file included from C:\Users\User\CLionProjects\ex2.2\sortedList.h:5,
                 from C:\Users\User\CLionProjects\ex2.2\main.cpp:2:
C:\Users\User\CLionProjects\ex2.2\dummy.h:14:5: note: candidate: 'Dummy::Dummy(int)'
     Dummy(int num) {
     ^~~~~
C:\Users\User\CLionProjects\ex2.2\dummy.h:14:5: note:   candidate expects 1 argument, 0 provided
C:\Users\User\CLionProjects\ex2.2\dummy.h:11:7: note: candidate: 'constexpr Dummy::Dummy(const Dummy&)'
 class Dummy {
       ^~~~~
C:\Users\User\CLionProjects\ex2.2\dummy.h:11:7: note:   candidate expects 1 argument, 0 provided
mingw32-make.exe[3]: *** [CMakeFiles\exe_name.dir\build.make:81: CMakeFiles/exe_name.dir/main.cpp.obj] Error 1
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:94: CMakeFiles/exe_name.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:101: CMakeFiles/exe_name.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:136: exe_name] Error 2

【问题讨论】:

  • 请将您的问题edit 复制粘贴到 fullcomplete 构建输出。另外,请在显示的代码中出现错误的行中添加 cmets。如果可能,请尝试发送minimal reproducible example 向我们展示哪个复制了错误,仅此而已。
  • 在一个不相关的注释上,using namespace std; is a bad habit 在普通源文件中,在头文件中它更糟糕。
  • 顺便说一句,当head 未初始化时,您的LinkedList(T value) 构造函数将取消引用head
  • 我该如何解决这个问题? >.
  • 一个附带问题:在LinkedList(T value)构造函数开头的赋值指令head-&gt;data = value;中我们应该期望head的值是多少?

标签: c++ class templates


【解决方案1】:

好吧,这很有趣。看看这里:

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

当你在节点中创建一个 T 时,你是怎么做的?那么你必须调用它的构造函数。现在让我们看看你的Dummy。它没有默认构造函数,您必须使用Dummy(int) 调用它。因此,在 Node 中,您的 T 现在是 Dummy 必须使用此构造函数构造。但是如何!?

这是您向编译器提出的难题。编译器以一种奇怪但明智的方式回答了。我无法构造这个Node,因为默认构造函数已被删除。它被删除是因为它的T (Dummy) 不能被默认构造。

解决此问题的一个潜在方法是向Node 添加一个新构造函数,该构造函数获取并复制T,又名:

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

你应该尝试一下。


一点旁注。你有问题。考虑一下如何添加后续节点,以及何时构建第一个节点。您的构造函数 LinkedList(T value) { 具有未定义的行为,因为它取消了对空指针的引用。

【讨论】:

  • 这是一个很好的答案,但是!创建一个 SortedList 是我作业的一部分,我不能假设我的 T 将有一个无参数的构造函数。我的假人只是检查它将如何工作。我该如何克服呢?不给假人添加任何东西(因为正如我所说,我不能假设它)
  • @SpaceNugget 所以你不能假设这些项目会有一个默认的构造函数?在这种情况下,您需要查看std::list::emplace 的工作原理。
  • @SpaceNugget 使用此处建议的解决方案,并在您的LinkedList::add 函数中使用new Node(value)。节点中的数据将不再需要默认构造。
  • @SpaceNugget • 您可能错过了可能的解决方法是...部分答案。另一种选择:struct Node { std::unique_ptr&lt;T&gt; data; Node* next; }; 或者如果其他人拥有对象 struct Node { T* data; Node* next; };
  • @Someprogrammerdude 但它需要被复制构造,希望这是可以假设的关于这个数据结构的用户的东西......
猜你喜欢
  • 2015-10-25
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2014-04-16
  • 1970-01-01
  • 2021-01-30
相关资源
最近更新 更多