Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ "No appropriate default constructor available"

Tags:

c++

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

When I compile I get the error listed above. How do I need to pass the array to the linked list? Thanks! (The code in question is marked by **, please remove if 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;
}
like image 794
Drew Avatar asked Feb 24 '23 20:02

Drew


2 Answers

The problem is in this part of insert:

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

where the node contains an Object. To construct that, the Object needs a default constructor.

Perhaps you can add a constructor to node that copies the value it has to store? That would make it, for example:

node<Object> *temp = new node<Object>(x, NULL);
like image 137
Bo Persson Avatar answered Mar 08 '23 16:03

Bo Persson


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

This line in SinglyLinkedList::insert causes the error I assume. The problem is, that your node struct looks like this:

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

That Type value; will be default constructed by the new node<Object> call. Provide an appropriate constructor for the node struct and you should be fine.

like image 45
Xeo Avatar answered Mar 08 '23 17:03

Xeo