[英]Segmentation Fault on Singlly Linked List
I am having an issue with segmentation fault that I haven't been able to fix.我遇到了无法修复的分段错误问题。 I am hoping for some insight as to why it might be happening.
我希望对它可能发生的原因有所了解。 This is a linked list implemented for taking strings and allocating each character to a node in the list.
这是一个链表,用于获取字符串并将每个字符分配给列表中的一个节点。 The segmentation fault points to the first line in my copy constructor, that creates the new Node.
分段错误指向我的复制构造函数中的第一行,它创建了新的 Node.js 文件。 Code included below.
代码包含在下面。
SLLString.cpp SLLString.cpp
#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字符串.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);
};
Main主要的
#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;
}
Node Class节点类
#include <iostream>
#include <cstddef>
using namespace std;
class Node{
public:
char data = 0;
Node *next = NULL;
};
SLLString
's copy assignment operator has several problems. SLLString
的复制赋值运算符有几个问题。 The one you're seeing is from the result = SLLString(holder);
你看到的是
result = SLLString(holder);
line, which will recursively call the copy assignment operator, eventually running out of stack space.行,它将递归调用复制赋值运算符,最终耗尽堆栈空间。
Other problems stem from the use of result
(you should be modifying the this
object, not a new one).其他问题源于
result
的使用(您应该修改this
对象,而不是新对象)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.