C++ 自定义类构造中的分段错误
因此,我正在编写一个浏览器选项卡结构,我有5个自定义类、堆栈、LinkedList、节点(用于链表)、选项卡和浏览器。 LinkedList和Stack本身可以正常工作,但当我在浏览器的构造函数中构造LinkedList时,会出现错误。因此,我主要只调用Browser()。以下是代码:C++ 自定义类构造中的分段错误,c++,constructor,segmentation-fault,C++,Constructor,Segmentation Fault,因此,我正在编写一个浏览器选项卡结构,我有5个自定义类、堆栈、LinkedList、节点(用于链表)、选项卡和浏览器。 LinkedList和Stack本身可以正常工作,但当我在浏览器的构造函数中构造LinkedList时,会出现错误。因此,我主要只调用Browser()。以下是代码: LinkedList<T>::LinkedList(){ front=NULL; back=NULL; }` Node<T>::Node(){ prev=NULL;
LinkedList<T>::LinkedList(){
front=NULL;
back=NULL;
}`
Node<T>::Node(){
prev=NULL;
next=NULL;
data=T();
}
Stack<T>::Stack(int capacity){ //capacity is optional, this is the default constructor.
this->capacity=capacity;
this->size=0;
this->items=new T[capacity];
}
LinkedList::LinkedList(){
front=NULL;
back=NULL;
}`
Node::Node(){
prev=NULL;
next=NULL;
数据=T();
}
Stack::Stack(int-capacity){//capacity是可选的,这是默认构造函数。
这->容量=容量;
这个->大小=0;
此->项目=新T[容量];
}
`
Browser(){
选中=空;
//开始打印浏览器链接构造
pages=LinkedList();//此行给出了错误。
closedPages=Stack();
货币指数=-1;
tab_计数=0;
}
Tab(){
当前页面=”;
prevPages=Stack();
下一页=堆栈();
封闭指数=-1;
}
更有趣的是,当我进行打印插入时,我看到的是它首先开始并完成链接构造(其间不进行任何选项卡构造),然后进行堆栈和选项卡构造,然后打印“浏览器链接构造开始”然后进入并完成另一个链路构造,然后给出seg故障。所以它两次连接施工,即使我希望它只做一次也很难
谢谢,如果是非常简单/愚蠢的事情,请提前道歉
EDIT1:完整的LinkedList代码
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#include <iostream>
#include <cstddef>
#include <stdexcept>
#include "Node.hpp"
using namespace std;
template <class T>
class LinkedList {
private:
/* pointer to the first node */
Node<T>* front;
/* pointer to the last node */
Node<T>* back;
public:
LinkedList();
LinkedList(const LinkedList<T>& ll);
LinkedList<T>& operator=(const LinkedList<T>& ll);
~LinkedList();
/* returns the first node of the linked list */
Node<T>& getFront() const;
/* returns the last node of the linked list */
Node<T>& getBack() const;
/* returns the node in given "pos"ition of the linked list */
Node<T>& getNodeAt(int pos) const;
/* returns the pointer of the node in given
"pos"ition of the linked list */
Node<T>* getNodePtrAt(int pos) const;
/* inserts a new node containing "data"
after the node "prev"
*/
void insert(Node<T>* prev, const T& data);
/* inserts a new node containing "data"
at "pos"ition in the linked list
*/
void insertAt(int pos, const T& data);
/* erases the given "node" from the linked list */
void erase(Node<T>* node);
/* erases the node in given "pos"ition from the linked list */
void eraseFrom(int pos);
/* clears the contents of the linked list */
void clear();
/* inserts a new node containing "data"
to the front of the linked list
*/
void pushFront(const T& data);
/* inserts a new node containing "data"
to the back of the linked list
*/
void pushBack(const T& data);
/* removes the first node */
void popFront();
/* removes the last node */
void popBack();
/* returns true if the list is empty, false otherwise */
bool isEmpty() const;
/* returns the number of items in the list */
size_t getSize() const;
/* prints the contents of the linked list
one node data per line
assumes the objects in the node have operator<< overloaded
*/
void print() const;
};
template <class T>
LinkedList<T>::LinkedList(){
cout << "list construction starts" << endl;
front=NULL;
back=NULL;
cout << "list construction ends" << endl;
}
//COPY AND ASSIGMENT
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& ll){
*this=ll;
}
template<class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& ll){
clear();
Node<T>* temp=&(ll.getFront());
while(temp->getNext()!=NULL){
pushBack(temp->getData());
temp->setNext(temp->getNext());
}
pushBack(temp->getData());
return *this;
}
template<class T>
LinkedList<T>::~LinkedList(){
clear();
}
template <class T>
Node<T>& LinkedList<T>::getFront() const{
return *front;
}
template <class T>
Node<T>& LinkedList<T>::getBack() const{
return *back;
}
template <class T>
Node<T>& LinkedList<T>::getNodeAt(int pos) const{
if(pos<0 or pos>=getSize()){
throw out_of_range("Bad Input");
}
Node<T>* retval=front;
for(int i=0;i<pos;i++){
retval=retval->getNext();
}
return *retval;
}
template <class T>
Node<T>* LinkedList<T>::getNodePtrAt(int pos) const{
if(pos<0 or pos>getSize()){
throw out_of_range("Bad Input");
}
Node<T>* retval=front;
for(int i=0;i<pos;i++){
retval=retval->getNext();
}
return retval;
}
template <class T>
void LinkedList<T>::insert(Node<T>* prev,const T& data){
if(prev==NULL){
pushFront(data);
}
else if(prev==back){
pushBack(data);
}
else{
Node<T>* newNode=new Node<T>();
newNode->setData(data);
prev->getNext()->setPrev(newNode);
newNode->setNext(prev->getNext());
newNode->setPrev(prev);
prev->setNext(newNode);
}
}
template <class T>
void LinkedList<T>::insertAt(int pos,const T& data){
if(pos==0){
pushFront(data);
}
else if(pos==getSize()){
pushBack(data);
}
else{
Node<T>* tmp=getNodePtrAt(pos);
Node<T> newNode;
newNode.setData(data);
tmp->getPrev()->setNext(&newNode);
newNode.setNext(tmp);
newNode.setPrev(tmp->getPrev());
tmp->setPrev(&newNode);
}
}
template <class T>
void LinkedList<T>::pushFront(const T& data){
Node<T>* newNode=new Node<T>();
newNode->setData(data);
if(front==NULL){
front=newNode;
back=newNode;
}
else {
newNode->setNext(front);
front->setPrev(newNode);
front=newNode;
newNode->setPrev(NULL);
}
}
template <class T>
void LinkedList<T>::pushBack(const T& data){
Node<T>* newNode=new Node<T>();
newNode->setData(data);
if(front==NULL){
front=newNode;
back=newNode;
}
else {
newNode->setPrev(back);
back->setNext(newNode);
back=newNode;
newNode->setNext(NULL);
}
}
template <class T>
void LinkedList<T>::erase(Node<T>* node){
if(node==front){
popFront();
}
if(node==back){
popBack();
}
else {
node->getNext()->setPrev(node->getPrev());
node->getPrev()->setNext(node->getNext());
node->setNext(NULL); node->setPrev(NULL);
}
delete node;
}
template <class T>
void LinkedList<T>::eraseFrom(int pos){
Node<T>* tmp=getNodePtrAt(pos);
erase(tmp);
}
template <class T>
void LinkedList<T>::clear(){
while(!isEmpty()){
popFront();
}
}
template <class T>
void LinkedList<T>::popFront(){
Node<T>* tmp;
tmp=front;
if(front==back){
front=NULL;
back=NULL;
}
else{
front=front->getNext();
front->setPrev(NULL);
}
delete tmp;
}
template <class T>
void LinkedList<T>::popBack(){
Node<T>* tmp;
tmp=back;
if(front==back){
front=NULL;
back=NULL;
}
else {
back=back->getPrev();
back->setNext(NULL);
}
delete tmp;
}
template <class T>
bool LinkedList<T>::isEmpty() const{
return front==NULL;
}
template <class T>
size_t LinkedList<T>::getSize() const{
if(front==NULL){
return 0;
}
size_t size=1;
Node<T>* current=front;
while(current->getNext()!=NULL){
size++;
current=current->getNext();
}
return size;
}
template <class T>
void LinkedList<T>::print() const{
Node<T>* current=front;
if(front!=NULL){
while(current->getNext()!=NULL){
cout << current->getData() << endl;
current=current->getNext();
}
cout << current->getData() << endl;
}
}
#endif
\ifndef\u LINKEDLIST\u H_
#定义链接列表_
#包括
#包括
#包括
#包括“Node.hpp”
使用名称空间std;
模板
类链接列表{
私人:
/*指向第一个节点的指针*/
节点*前端;
/*指向最后一个节点的指针*/
节点*返回;
公众:
LinkedList();
LinkedList(常量LinkedList&ll);
LinkedList&operator=(常量LinkedList&ll);
~LinkedList();
/*返回链表的第一个节点*/
节点&getFront()常量;
/*返回链表的最后一个节点*/
节点&getBack()常量;
/*返回链表中给定位置的节点*/
Node&getNodeAt(int-pos)const;
/*返回给定区域中节点的指针
链接列表的“位置”*/
Node*getNodePtrAt(int pos)const;
/*插入包含“数据”的新节点
在节点“prev”之后
*/
无效插入(节点*上一个、常数和数据);
/*插入包含“数据”的新节点
在链接列表中的“位置”
*/
无效插入(内部位置、常量和数据);
/*从链表中删除给定的“节点”*/
无效擦除(节点*节点);
/*从链表中删除给定“位置”中的节点*/
无效删除(int pos);
/*清除链接列表的内容*/
无效清除();
/*插入包含“数据”的新节点
到链接列表的前面
*/
无效前推(常数T和数据);
/*插入包含“数据”的新节点
到链接列表的后面
*/
无效回推(常数T和数据);
/*删除第一个节点*/
void popFront();
/*删除最后一个节点*/
void popBack();
/*如果列表为空,则返回true,否则返回false*/
bool isEmpty()常量;
/*返回列表中的项目数*/
大小\u t getSize()常量;
/*打印链接列表的内容
每行一个节点数据
假设节点中的对象具有operatorgetData());
归还*这个;
}
模板
LinkedList::~LinkedList(){
清除();
}
模板
节点和链接列表::getFront()常量{
返回*前端;
}
模板
节点和链接列表::getBack()常量{
返回*返回;
}
模板
节点和链接列表::getNodeAt(int pos)const{
如果(pos=getSize()){
抛出_范围内的_(“错误输入”);
}
节点*retval=front;
对于(int i=0;igetNext();
}
返回*返回;
}
模板
节点*LinkedList::getNodePtrAt(int-pos)常量{
if(posgetSize()){
抛出_范围内的_(“错误输入”);
}
节点*retval=front;
对于(int i=0;igetNext();
}
返回返回;
}
模板
void LinkedList::insert(节点*prev、常量T和数据){
if(prev==NULL){
推前(数据);
}
else if(prev==返回){
推回(数据);
}
否则{
Node*newNode=newNode();
新建节点->设置数据(数据);
prev->getNext()->setPrev(newNode);
newNode->setNext(prev->getNext());
newNode->setPrev(prev);
prev->setNext(新建节点);
}
}
模板
void LinkedList::insertAt(int pos、const T和data){
如果(位置==0){
推前(数据);
}
else if(pos==getSize()){
推回(数据);
}
否则{
Node*tmp=getNodePtrAt(pos);
节点newNode;
newNode.setData(数据);
tmp->getPrev()->setNext(&newNode);
newNode.setNext(tmp);
setPrev(tmp->getPrev());
tmp->setPrev(&newNode);
}
}
模板
void LinkedList::pushFront(常量和数据){
Node*newNode=newNode();
新建节点->设置数据(数据);
if(front==NULL){
front=newNode;
back=newNode;
}
否则{
新建节点->设置下一步(前);
前->设置预览(新节点);
front=newNode;
newNode->setPrev(空);
}
}
模板
void LinkedList::pushBack(常量和数据){
Node*newNode=newNode();
新建节点->设置数据(数据);
if(front==NULL){
front=newNode;
back=newNode;
}
否则{
newNode->setPrev(返回);
后退->设置下一步(新建节点);
back=newNode;
新建节点->设置下一步(空);
}
}
模板
无效链接列表::擦除(节点*节点){
如果(节点==前){
popFront();
}
如果(节点==返回){
弹回();
}
否则{
node->getNext()->setPrev(node->getPrev());
node->getPrev()->setNext(node->getNext());
节点->设置下一步(NULL);节点->设置上一步(NULL);
}
删除节点;
}
模板
void LinkedList::eraseFrom(int-pos){
Node*tmp=getNodePtrAt(pos);
擦除(tm)
#ifndef _LINKEDLIST_H_
#define _LINKEDLIST_H_
#include <iostream>
#include <cstddef>
#include <stdexcept>
#include "Node.hpp"
using namespace std;
template <class T>
class LinkedList {
private:
/* pointer to the first node */
Node<T>* front;
/* pointer to the last node */
Node<T>* back;
public:
LinkedList();
LinkedList(const LinkedList<T>& ll);
LinkedList<T>& operator=(const LinkedList<T>& ll);
~LinkedList();
/* returns the first node of the linked list */
Node<T>& getFront() const;
/* returns the last node of the linked list */
Node<T>& getBack() const;
/* returns the node in given "pos"ition of the linked list */
Node<T>& getNodeAt(int pos) const;
/* returns the pointer of the node in given
"pos"ition of the linked list */
Node<T>* getNodePtrAt(int pos) const;
/* inserts a new node containing "data"
after the node "prev"
*/
void insert(Node<T>* prev, const T& data);
/* inserts a new node containing "data"
at "pos"ition in the linked list
*/
void insertAt(int pos, const T& data);
/* erases the given "node" from the linked list */
void erase(Node<T>* node);
/* erases the node in given "pos"ition from the linked list */
void eraseFrom(int pos);
/* clears the contents of the linked list */
void clear();
/* inserts a new node containing "data"
to the front of the linked list
*/
void pushFront(const T& data);
/* inserts a new node containing "data"
to the back of the linked list
*/
void pushBack(const T& data);
/* removes the first node */
void popFront();
/* removes the last node */
void popBack();
/* returns true if the list is empty, false otherwise */
bool isEmpty() const;
/* returns the number of items in the list */
size_t getSize() const;
/* prints the contents of the linked list
one node data per line
assumes the objects in the node have operator<< overloaded
*/
void print() const;
};
template <class T>
LinkedList<T>::LinkedList(){
cout << "list construction starts" << endl;
front=NULL;
back=NULL;
cout << "list construction ends" << endl;
}
//COPY AND ASSIGMENT
template<class T>
LinkedList<T>::LinkedList(const LinkedList<T>& ll){
*this=ll;
}
template<class T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& ll){
clear();
Node<T>* temp=&(ll.getFront());
while(temp->getNext()!=NULL){
pushBack(temp->getData());
temp->setNext(temp->getNext());
}
pushBack(temp->getData());
return *this;
}
template<class T>
LinkedList<T>::~LinkedList(){
clear();
}
template <class T>
Node<T>& LinkedList<T>::getFront() const{
return *front;
}
template <class T>
Node<T>& LinkedList<T>::getBack() const{
return *back;
}
template <class T>
Node<T>& LinkedList<T>::getNodeAt(int pos) const{
if(pos<0 or pos>=getSize()){
throw out_of_range("Bad Input");
}
Node<T>* retval=front;
for(int i=0;i<pos;i++){
retval=retval->getNext();
}
return *retval;
}
template <class T>
Node<T>* LinkedList<T>::getNodePtrAt(int pos) const{
if(pos<0 or pos>getSize()){
throw out_of_range("Bad Input");
}
Node<T>* retval=front;
for(int i=0;i<pos;i++){
retval=retval->getNext();
}
return retval;
}
template <class T>
void LinkedList<T>::insert(Node<T>* prev,const T& data){
if(prev==NULL){
pushFront(data);
}
else if(prev==back){
pushBack(data);
}
else{
Node<T>* newNode=new Node<T>();
newNode->setData(data);
prev->getNext()->setPrev(newNode);
newNode->setNext(prev->getNext());
newNode->setPrev(prev);
prev->setNext(newNode);
}
}
template <class T>
void LinkedList<T>::insertAt(int pos,const T& data){
if(pos==0){
pushFront(data);
}
else if(pos==getSize()){
pushBack(data);
}
else{
Node<T>* tmp=getNodePtrAt(pos);
Node<T> newNode;
newNode.setData(data);
tmp->getPrev()->setNext(&newNode);
newNode.setNext(tmp);
newNode.setPrev(tmp->getPrev());
tmp->setPrev(&newNode);
}
}
template <class T>
void LinkedList<T>::pushFront(const T& data){
Node<T>* newNode=new Node<T>();
newNode->setData(data);
if(front==NULL){
front=newNode;
back=newNode;
}
else {
newNode->setNext(front);
front->setPrev(newNode);
front=newNode;
newNode->setPrev(NULL);
}
}
template <class T>
void LinkedList<T>::pushBack(const T& data){
Node<T>* newNode=new Node<T>();
newNode->setData(data);
if(front==NULL){
front=newNode;
back=newNode;
}
else {
newNode->setPrev(back);
back->setNext(newNode);
back=newNode;
newNode->setNext(NULL);
}
}
template <class T>
void LinkedList<T>::erase(Node<T>* node){
if(node==front){
popFront();
}
if(node==back){
popBack();
}
else {
node->getNext()->setPrev(node->getPrev());
node->getPrev()->setNext(node->getNext());
node->setNext(NULL); node->setPrev(NULL);
}
delete node;
}
template <class T>
void LinkedList<T>::eraseFrom(int pos){
Node<T>* tmp=getNodePtrAt(pos);
erase(tmp);
}
template <class T>
void LinkedList<T>::clear(){
while(!isEmpty()){
popFront();
}
}
template <class T>
void LinkedList<T>::popFront(){
Node<T>* tmp;
tmp=front;
if(front==back){
front=NULL;
back=NULL;
}
else{
front=front->getNext();
front->setPrev(NULL);
}
delete tmp;
}
template <class T>
void LinkedList<T>::popBack(){
Node<T>* tmp;
tmp=back;
if(front==back){
front=NULL;
back=NULL;
}
else {
back=back->getPrev();
back->setNext(NULL);
}
delete tmp;
}
template <class T>
bool LinkedList<T>::isEmpty() const{
return front==NULL;
}
template <class T>
size_t LinkedList<T>::getSize() const{
if(front==NULL){
return 0;
}
size_t size=1;
Node<T>* current=front;
while(current->getNext()!=NULL){
size++;
current=current->getNext();
}
return size;
}
template <class T>
void LinkedList<T>::print() const{
Node<T>* current=front;
if(front!=NULL){
while(current->getNext()!=NULL){
cout << current->getData() << endl;
current=current->getNext();
}
cout << current->getData() << endl;
}
}
#endif
pages = LinkedList<Tab>();