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