Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;链表包含()函数 我对C++是很新的,并且在插入链接()函数中使用链接表有困难。以下是我收到的代码,从以下开始: #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; }_C++_Templates_Singly Linked List - Fatal编程技术网

C++;链表包含()函数 我对C++是很新的,并且在插入链接()函数中使用链接表有困难。以下是我收到的代码,从以下开始: #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; }

C++;链表包含()函数 我对C++是很新的,并且在插入链接()函数中使用链接表有困难。以下是我收到的代码,从以下开始: #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; },c++,templates,singly-linked-list,C++,Templates,Singly Linked List,我的问题是我遇到了以下错误,我不知道如何修复它,也不知道它意味着什么: 1>c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_tpt.h(184): error C2512: 'mylist<T>::node' : no appropriate default constructor available 1> with 1> [ 1>

我的问题是我遇到了以下错误,我不知道如何修复它,也不知道它意味着什么:

1>c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_tpt.h(184): error C2512: 'mylist<T>::node' : no appropriate default constructor available
1>          with
1>          [
1>              T=std::string
1>          ]
1>          c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_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:\users\jaysen\documents\data structures\lab 2\lab 2\mylist_main.cpp(20) : see reference to class template instantiation 'mylist<T>' being compiled
1>          with
1>          [
1>              T=std::string
1>          ]
1>c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist\u tpt.h(184):错误C2512:“mylist::node”:没有合适的默认构造函数可用
1> 与
1>          [
1> T=std::string
1>          ]
1> c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist\u tpt.h(182):编译类模板成员函数“void mylist::insert(const T&,unsigned int)”时
1> 与
1>          [
1> T=std::string
1>          ]
1> c:\users\jaysen\documents\data structures\lab 2\lab 2\mylist\u main.cpp(20):请参阅正在编译的类模板实例化“mylist”的参考
1> 与
1>          [
1> T=std::string
1>          ]

提前谢谢你能给我的任何帮助

尝试将
const
cualifier添加到
empty
函数中:

bool empty() const { return head_ptr == nullptr;}

此错误与问题标题无关。请重新阅读错误,尤其是文件名和行号。您的想法非常有效!我现在的插入函数有问题。。。我已将问题中的代码更改为新代码。如@JoachimPileborg所述,错误消息包含修复错误所需的所有信息。是的,我必须承认这个错误信息很难阅读,但是再多练习一下,你就会习惯阅读它了。关于你的新问题,我想(因为你还没有发布行号)在某个地方需要一个节点类的默认构造函数(可能是在复制自定义列表时),但是,由于问题主题的改变,我认为最好打开一个新问题。
bool empty() const { return head_ptr == nullptr;}