C ++ "There is no suitable default constructor"

I am trying to create a linked list of arrays without using STL. However, I am having difficulty passing the array to my Linked List ...

When compiling, I get the above error. How do I pass an array to a linked list? Thank you (This code is marked **, please delete it when testing.)

SinglyLinkedList.h

#pragma once #ifndef SinglyLinkedList_h #define SinglyLinkedList_h #include<iostream> template <typename Type> struct node { Type value; node *next; }; template <typename Object> class SinglyLinkedList { private: node<Object> *head; public: SinglyLinkedList(); ~SinglyLinkedList(); bool insert(Object x); bool empty(); }; template <typename Object> SinglyLinkedList<Object>::SinglyLinkedList() { this->head = NULL; } template <typename Object> bool SinglyLinkedList<Object>::insert(Object x) { node<Object> *temp = new node<Object>; temp->value = x; temp->next = NULL; if (this->head==NULL) { this->head = temp; } else { node<Object> *S = this->head, *P = S; while ((S->value < temp->value)&&(S != NULL)) { S = S->next; P = S; } if(S == NULL) temp->next = P; else { temp->next = S; P->next = temp; } } return true; } template <typename Object> bool SinglyLinkedList<Object>::empty() { if(this->head == NULL) return true; else return false; } template <typename Object> SinglyLinkedList<Object>::~SinglyLinkedList() { delete this->head; } #endif 

DynamicArrayClass.h

 #pragma once #ifndef DynamicArrayClass_h #define DynamicArrayClass_h #include<iostream> template <class T> class DynamicArrayClass { private: T *array; int size, numItems; public: DynamicArrayClass(int newSize) { size = newSize; numItems=0; array = new T[size]; } int GetSize(){ return size;} int GetNumItems() const { return numItems; } bool isEmpty() const { return numItems==0; } bool isFull() const { return numItems==size; } bool addItem (const T &object) { if(isFull()) { return false; } else { array[numItems++] = object; return true; } } const T& getItem(int index) {return array[index];} void makeEmpty() { numItems = 0; } ~DynamicArrayClass() { if(array !NULL) delete [] array; } }; #endif 

main.cpp

 #include "DynamicArrayClass.h" #include "SinglyLinkedList.h" #include "stopwatch.h" #include<iostream> int main() { int totalCapacity = 0; int arrayAddSize = 0; while(totalCapacity < 10000) { if(totalCapacity==0) { DynamicArrayClass<int> *array1 = new DynamicArrayClass<int>(25); totalCapacity = 25; SinglyLinkedList<DynamicArrayClass<int>> *list = new SinglyLinkedList<DynamicArrayClass<int>>(); for(int i = 0; i<25; i++) { array1->addItem(1); } **list->insert(*array1);** } else { arrayAddSize = (totalCapacity/2); totalCapacity = totalCapacity + arrayAddSize; DynamicArrayClass<int> *array = new DynamicArrayClass<int>(arrayAddSize); SinglyLinkedList<DynamicArrayClass<int>> *list = new SinglyLinkedList<DynamicArrayClass<int>>(); for(int i=0; i <arrayAddSize; i++) { array->addItem(1); } } } return 0; } 
+4
source share
2 answers

The problem is this part of insert :

 node<Object> *temp = new node<Object>; 

where node contains a Object . To build this, Object requires a default constructor.

Perhaps you can add a constructor to the node that copies the value that it should store? This would do it, for example:

 node<Object> *temp = new node<Object>(x, NULL); 
+4
source
 node<Object> *temp = new node<Object>; 

This line in SinglyLinkedList::insert causes the error that I am assuming. The problem is that your node structure looks like this:

 template <typename Type> struct node { Type value; node *next; }; 

That Type value; will be configured by default by calling new node<Object> . Provide the appropriate constructor for the node structure, and you should be fine.

+4
source

All Articles