I created a Node class for a linked list in C++:
template <class T> class Node {
public:
T val;
Node* next;
Node(T val) {
this->val = val;
this->next = nullptr;
}
~Node() {
Node<T>* temp = this->next;
if (temp != nullptr && temp->next != nullptr)
delete(temp->next);
}
};
And when trying tp initialize it:
definition:
Node<Device>* devices;
code (in function):
this->devices = new Node<Device>({ this->serial_counter, name });
I get this error:
Error C2512 'Device': no appropriate default constructor available Gym c:\users\amitm\onedrive\מסמכים\visual studio 2015\projects\gym\gym\node.h 7
Line 7:
Node(T val) {
Also, if needed, this is the "Device" constructor:
Device::Device(int id, char* name) {
this->id = id;
strcpy(this->name, name);
}
How can I fix this error? I looked on-line for over an hour and cannot find a solution that works for me.
The problem is that your value of type T
is initialized in your constructor, and you then try to assign a new value. If you want to remove the error message, you need to initialize your value yourself:
Node(T v)
: val(v)
, next(nullptr)
{}
You can have more information here.