gpt4 book ai didi

C++ "No appropriate default constructor available"

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:16 33 4
gpt4 key购买 nike

我想在不使用 STL 的情况下创建一个数组链表。但是,我在将数组传递到我的链接列表时遇到困难...

编译时出现上面列出的错误。我需要如何将数组传递给链表?谢谢! (有问题的代码有**标记,如果测试请去掉)

单链表.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

动态数组类.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

主要.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;
}

最佳答案

问题出在insert这部分:

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

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

也许您可以向 node 添加一个构造函数来复制它必须存储的值?这样就可以了,例如:

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

关于C++ "No appropriate default constructor available",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6401147/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com