This is my code :
#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
and when I try to compile it I get the error: use of deleted function 'LinkedList::Node::Node()'
I understand that it is probably because I can't know before-hand if I will get any arguments while creating the sorted list or not, how can I do that? To make it compile and run?
The Dummy class is like that:
#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
My main is:
#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;
}
The build log:
====================[ 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