c++ - 如何处理错误:使用已删除的函数 c++?
问题描述
这是我的代码:
#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
解决方案
好的,所以这是一个有趣的。看看这里:
template <class T>
class LinkedList {
struct Node {
T data; // Here you create a T
当你在节点中创建一个 T 时,你是怎么做的?那么你必须调用它的构造函数。现在让我们看看你的Dummy
. 它没有默认构造函数,您必须使用Dummy(int)
. 因此,在Node
你的T
which is now中Dummy
必须使用这个构造函数来构造。但是怎么办!?
这是您向编译器提出的难题。编译器以一种奇怪但明智的方式回答了。我无法构造它Node
,因为默认构造函数已被删除。它被删除是因为它的T
( Dummy
) 不能被默认构造。
一个潜在的解决方法是向 中添加一个新的构造函数Node
,该构造函数接受并复制 a T
,也就是:
struct Node {
Node(const T &in) : data(in) {}
T data;
您应该对此进行一些试验。
一点旁注。你有问题。考虑一下如何添加后续节点,以及何时构建第一个节点。您的构造函数LinkedList(T value) {
具有未定义的行为,因为它取消引用空指针。
推荐阅读
- python - 如何将 float64 值转换为等效的日期时间格式?
- php - Laravel Livewire 分页链接不起作用
- ios - 将静态库 (Rust .a) 导入 iOS 中的 Flutter 项目
- javascript - 创建时如何在 html 中显示本地存储中的数据
- r - 无法在 R-studio 中安装 R 包源
- apache-spark - 优化从 s3 存储桶中的分区镶木地板文件中读取
- javascript - 在 React 应用程序中检索 JSON 数据时的不同结果
- google-cloud-platform - 获取 GCP 日志查看器以突出显示自定义标签
- pandas - 如果两个键列具有不同的值,可以合并两个表吗?
- reactjs - 如何使用 facebook javasscript login sdk 获取代码而不是访问令牌?