4

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;
}
1
  • 3
    A lot of code to dump on us, but +1 for using the homework tag without being asked.
    – Chris Lutz
    Jun 19, 2011 at 7:30

2 Answers 2

4

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);
4
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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.