C++ 单链表上的分段错误
我有一个分割错误的问题,我无法修复。我希望能对为什么会发生这种情况有所了解。这是一个链表,用于获取字符串并将每个字符分配给列表中的一个节点。分段错误指向复制构造函数中创建新节点的第一行。代码包括在下面 SLLString.cppC++ 单链表上的分段错误,c++,list,nodes,C++,List,Nodes,我有一个分割错误的问题,我无法修复。我希望能对为什么会发生这种情况有所了解。这是一个链表,用于获取字符串并将每个字符分配给列表中的一个节点。分段错误指向复制构造函数中创建新节点的第一行。代码包括在下面 SLLString.cpp #include "SLLString.h" #include <iostream> using namespace std; SLLString::SLLString(const string &other) { N
#include "SLLString.h"
#include <iostream>
using namespace std;
SLLString::SLLString(const string &other)
{
Node *newNode = new Node();
^^^ //Segmentation Fault here
head = newNode;
newNode->data = other[0];
newNode->next;
for (int i = 1; i < other.size(); i++)
{
newNode->next = new Node;
newNode->next->data = other[i];
newNode = newNode->next;
counter++;
}
}
SLLString::~SLLString()
{
Node *currNode = head;
cout << "Delete...";
while (currNode != NULL)
{
head = currNode;
currNode = currNode->next;
cout << head->data << " ";
delete head;
}
cout << endl;
}
SLLString::SLLString(const SLLString &other)
{
Node *newNode = head;
Node *newNode2 = other.head;
while (newNode2 != NULL)
{
newNode->data = newNode2->data;
newNode = newNode->next;
newNode2 = newNode2->next;
}
}
SLLString &SLLString::operator=(const SLLString &other)
{
Node *travNode = other.head;
static SLLString result;
string holder;
while (travNode != NULL)
{
holder += travNode->data;
travNode = travNode->next;
}
result = SLLString(holder);
return result;
}
int SLLString::length()
{
int counter = 0;
Node *currNode = head;
if (currNode = NULL)
{
return 0;
}
while (currNode != NULL)
{
counter++;
currNode->next = currNode;
}
return counter;
}
void SLLString::erase(char c)
{
Node *currNode = head;
if (head = NULL)
{
cout << "List is empty.";
}
while (currNode != NULL)
{
if (currNode->data = c)
{
cout << "Removing: " << c << endl;
currNode->next = currNode->next->next;
currNode = currNode->next;
}
}
}
SLLString &SLLString::operator+=(const SLLString &other)
{
SLLString *result;
result = this;
*result += other;
return *result;
}
char &SLLString::operator[](const int n)
{
Node *current = new Node;
static char c;
int compare = n;
for (current = head; current != NULL; current = current->next)
{
if (compare = counter)
{
c = current->data;
}
}
return c;
}
ostream &operator<<(ostream &os, const SLLString strng)
{
Node *newNode;
newNode = strng.head;
if (newNode != NULL)
{
cout << newNode->data;
newNode = newNode->next;
}
while (newNode != NULL)
{
cout << "->" << newNode->data;
newNode = newNode->next;
}
return os;
}
#包括“SLLString.h”
#包括
使用名称空间std;
SLLString::SLLString(常量字符串和其他)
{
Node*newNode=newNode();
^^^//此处存在分段错误
头=新节点;
新建节点->数据=其他[0];
新建节点->下一步;
对于(int i=1;i下一步=新建节点;
新建节点->下一步->数据=其他[i];
newNode=newNode->next;
计数器++;
}
}
SLLString::~SLLString()
{
节点*当前节点=头部;
下一步;
cout数据;
newNode=newNode->next;
newNode2=newNode2->next;
}
}
SLLString和SLLString::运算符=(常量SLLString和其他)
{
Node*travNode=other.head;
静态SLLString结果;
绳夹;
while(travNode!=NULL)
{
holder+=travNode->data;
travNode=travNode->next;
}
结果=SLLString(支架);
返回结果;
}
int SLLString::length()
{
int计数器=0;
节点*当前节点=头部;
if(currNode=NULL)
{
返回0;
}
while(currNode!=NULL)
{
计数器++;
currNode->next=currNode;
}
返回计数器;
}
void SLLString::擦除(字符c)
{
节点*当前节点=头部;
if(head=NULL)
{
cout data=c)
{
下一步;
currNode=currNode->next;
}
}
}
SLLString和SLLString::运算符+=(常量SLLString和其他)
{
SLLString*结果;
结果=这个;
*结果+=其他;
返回*结果;
}
char&SLLString::运算符[](常量int n)
{
节点*当前=新节点;
静态字符c;
int比较=n;
对于(当前=头部;当前!=NULL;当前=当前->下一步)
{
如果(比较=计数器)
{
c=当前->数据;
}
}
返回c;
}
ostream&operatornext;
}
返回操作系统;
}
SLLString.h
#include "node.h"
class SLLString
{
public:
SLLString() { head = NULL; }; // fixed by initializing private variable --REMOVE THIS
SLLString(const string &other);
~SLLString(); //done
SLLString(const SLLString &other); //done
SLLString &operator=(const SLLString &other);
int length();
SLLString &operator+=(const SLLString &other);
char &operator[](const int n);
int findSubstring(const SLLString &substring);
void erase(char c);
Node *head = NULL;
int counter = 0;
friend ostream &operator<<(ostream &os, const SLLString strng);
};
#包括“node.h”
类SLLString
{
公众:
SLLString(){head=NULL;};//通过初始化私有变量修复--删除此
SLLString(常量字符串和其他);
~SLLString();//完成
SLLString(const SLLString&other);//完成
SLLString和运算符=(常量SLLString和其他);
int length();
SLLString和运算符+=(常量SLLString和其他);
字符和运算符[](常量int n);
int findSubstring(const SLLString和substring);
无效擦除(字符c);
Node*head=NULL;
int计数器=0;
friend ostream&operatorSLLString
的复制赋值运算符有几个问题。您看到的问题来自result=SLLString(holder);
行,它将递归调用复制赋值运算符,最终耗尽堆栈空间
其他问题源于使用result
(您应该修改此
对象,而不是新对象)。SLLString
的复制分配操作符有几个问题。您看到的问题来自result=SLLString(holder);
line,它将递归调用复制赋值运算符,最终耗尽堆栈空间
其他问题源于使用result
(您应该修改此
对象,而不是新对象).请编辑要包含的问题。例如,节点
定义缺失。节点
是什么?它的定义在哪里?更新为包含节点类,很抱歉忽略了这一点!请编辑要包含的问题。例如,节点
定义缺失。节点
在哪里它的定义?更新为包含节点类,很抱歉忽略了这一点!
#include <iostream>
#include <string>
#include "SLLString.h"
using namespace std;
int main(){
SLLString str("Hello world!");
SLLString newStr;
newStr = str;
newStr += SLLString(" CS@BC");
newStr[6] = 'W';
cout << newStr;
cout << newStr << endl; // Hello World! CS@BC
cout << newStr.length() << endl; //18
int loc = newStr.findSubstring("World");
cout << loc << endl; // 6
newStr.erase('l'); //erase the letter l.
cout << newStr << endl; // Heo Word! CS@BC
newStr.erase('C');
cout << newStr << endl; // Heo Word! S@B
return 0;
}
#include <iostream>
#include <cstddef>
using namespace std;
class Node{
public:
char data = 0;
Node *next = NULL;
};