C++没有默认构造函数?正在尝试为LinkedList创建Insert方法
C++ No Default Constructor? Trying to Create Insert Method for LinkedList
我真的是C++的新手,在使insert()函数与LinkedList一起工作时遇到了问题。这是我得到的代码,从开始
#include <iostream>
#include <cassert>
#include <stdexcept>
using namespace std;
template<typename T> class mylist;
template<typename T>
ostream& operator<< (ostream &out, const mylist<T> &l);
template <typename T>
class mylist {
public:
// node definition
struct node {
T data;
node* next_ptr;
// node constructor
node(const T &d, node* n):data(d),next_ptr(n){}
};
// alternative node definition
/*
class node {
public:
T data;
node* next_ptr;
// node constructor
node(const T&d, node* n):data(d),next_ptr(n){}
};
*/
// linked list head pointer
node* head_ptr;
//friend ostream& operator<< <>(ostream& out, const mylist<T>&l);
friend ostream& operator<< (ostream &out, const mylist<T> &l);
public:
// default constructor
mylist():head_ptr(nullptr) {} // head_ptr points nowhere
// adds element to the front of the linked list
void push_front(const T &elem) {
head_ptr = new node(elem, head_ptr);
}
// check if linked list is empty
bool empty() { return head_ptr == nullptr;}
// number of nodes in linked list
unsigned size() { return length();}
unsigned length() {
unsigned l = 0;
for(node* current = head_ptr; current != nullptr; current = current->next_ptr) {
++l;
}
return l;
}
// copy constructor
mylist(const mylist &other)
{
for(node* current_other = other.head_ptr;
current_other != nullptr;
current_other = current_other->next_ptr) {
this.push_back(current_other->data); // inefficient, but easy :)
}
}
// destructor
~mylist() {
node* tmp;
for(node* current = head_ptr;
current != nullptr;
current = tmp) {
tmp=current->next_ptr;
delete current;
}
}
// at accessor method (returns the element at the ith position in the linked list)
T& at(unsigned i){
unsigned l=0;
node* current;
for(current = head_ptr; current != nullptr; current = current->next_ptr) {
if(l == i)
break;
++l;
}
if (current == nullptr)
throw out_of_range("index i is out of range");
else
return current->data;
}
// bracket operator (returns the element at the ith position in the linked list)
T& operator[](unsigned i){
return at(i);
}
// adds element to the end of the linked list
void push_back(const T &elem) {
if(empty()) {
push_front(elem);
return;
}
node* last_ptr;
for(last_ptr = head_ptr; last_ptr->next_ptr != nullptr;
last_ptr = last_ptr->next_ptr);
last_ptr->next_ptr = new node(elem, nullptr);
}
// prints the contents of the linked list
void print_all(void) {
cout << "mylist{";
for(node* current_ptr = head_ptr;
current_ptr != nullptr;
current_ptr = current_ptr->next_ptr){
cout << current_ptr->data << " ";
}
cout << "}" << endl;
}
我正在尝试创建一个新函数insert(constT&elem,unsigned I)。以下代码的注释中描述了它的用途:
// inserts the element at position i in linked list.
// throws out of range error if position i not in list.
void insert (const T &elem, unsigned i) {
unsigned l=0;
node* current, prev;
for(current = head_ptr; current != nullptr; current = current->next_ptr) {
if(l == i)
break;
++l;
prev = current;
}
if (current == nullptr)
throw out_of_range("index i is out of range");
else
{
prev->next_ptr = new Node (elem, current);
}
}
我的问题是,我得到了以下错误,我不知道如何修复它,或者它意味着什么:
1>c:usersjaysendocumentsdata structureslab 2lab 2mylist_tpt.h(184): error C2512: 'mylist<T>::node' : no appropriate default constructor available
1> with
1> [
1> T=std::string
1> ]
1> c:usersjaysendocumentsdata structureslab 2lab 2mylist_tpt.h(182) : while compiling class template member function 'void mylist<T>::insert(const T &,unsigned int)'
1> with
1> [
1> T=std::string
1> ]
1> c:usersjaysendocumentsdata structureslab 2lab 2mylist_main.cpp(20) : see reference to class template instantiation 'mylist<T>' being compiled
1> with
1> [
1> T=std::string
1> ]
提前感谢你能给我的任何帮助!
错误在这一行:
node* current, prev;
这将current
声明为指向node
的指针,prev
声明为node
实例。像*
、&
或[]
这样的声明器每次只应用于一个标识符。这就是为什么最好把它们放在标识符旁边,而不是放在类型旁边:
node *current, *prev;
node* current, prev;
应为:
node *current, *prev;
正如您所写的,prev
不是一个指针。这是一个node
。
正如错误所说,没有办法以这种方式构建node
。(需要两个参数)
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- c++创建新的LinkedList类:Clear()方法指针被释放未分配
- 创建一个接受linkedlist作为输入的合并排序
- C++错误,显示创建链接列表调用"error LinkedList Interface is an inaccessable base of linkedlist"
- 合并两个LinkedList而不创建新的LinkedList
- C++没有默认构造函数?正在尝试为LinkedList创建Insert方法