C ++“没有合适的默认构造函数”

时间:2011-06-19 07:24:25

标签: c++

我正在尝试在不使用STL的情况下创建数组的链接列表。但是,我很难将数组传递给我的链接列表...

当我编译时,我得到上面列出的错误。如何将数组传递到链表?谢谢! (有问题的代码用**标记,如果测试请删除。)

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;
}

2 个答案:

答案 0 :(得分:4)

问题在于insert

的这一部分
node<Object> *temp = new node<Object>;

其中node包含Object。要构造它,Object需要一个默认的构造函数。

也许你可以添加一个构造函数给node复制它必须存储的值?这将使它成为例如:

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

答案 1 :(得分:4)

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

SinglyLinkedList::insert中的这一行会导致我假设的错误。问题是,您的node结构如下所示:

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

Type value;调用默认构建new node<Object>。为node结构提供适当的构造函数,你应该没问题。