指针上的操作符=可能导致mac上的分段错误
operator= on pointers possibly causing a segmentation fault on mac
我想写一个类集与链表存储整数。在我的Mac终端上编译并运行后,输出如下:
[]
[10]
[10, 20]
Segmentation fault: 11
但是我期望看到以下输出:
[]
[10]
[10, 20]
[10, 20]
[10, 20, 30]
我想知道这是一个问题与我的操作符=函数,或者它是我不能使用操作符=函数与指针?如果是这样,我应该如何纠正这个问题,使程序如我所期望的那样输出?我将非常感谢你的帮助。提前感谢!
#include <iostream>
using namespace std;
class Node {
public:
int value;
Node* next;
Node(int n, Node* ptr = NULL) : value(n), next(ptr) {}
};
class Set {
Node* head;
friend ostream& operator<<(ostream&, const Set&);
public:
Set() : head(NULL) {}
Set(const Set& another){ *this = another; }
~Set();
Set& operator+=(const int&);
Set& operator=(const Set&);
};
int main() {
int num1 = 10;
int num2 = 20;
int num3 = 30;
Set set1;
cout << set1;
Set* set2;
set1 += num1;
cout << set1;
set1 += num2;
cout << set1;
set2 = new Set(set1);
cout << *set2;
*set2 += num3;
cout << *set2;
delete set2;
return 0;
}
Set::~Set() {
Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
delete temp;
}
}
Set& Set::operator+=(const int& aNum) {
if (head == NULL) {
head = new Node(aNum);
return *this;
}
Node* previous = head;
Node* current = head->next;
while (current != NULL) {
previous = current;
current = current->next;
}
previous->next = new Node(aNum);
return *this;
}
Set& Set::operator=(const Set& another) {
if (this != &another) {
Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
delete temp;
}
Node* anotherCurrent = another.head;
while (anotherCurrent != NULL) {
*this += anotherCurrent->value;
anotherCurrent = anotherCurrent->next;
}
}
return *this;
}
ostream& operator<<(ostream& os, const Set& s) {
os << "[";
for (Node* p = s.head; p != NULL; p = p->next) {
os << p->value;
if (p->next != NULL)
os << ", ";
}
os << "]" << endl;
return os;
}
在复制之前删除之前的列表时,您必须将head
设置为NULL
,否则+=
操作符将使用head
,并且它当前未分配,但不是NULL
Set& Set::operator=(const Set& another) {
if (this != &another) {
Node* current = head;
while (current != NULL) {
Node* temp = current;
current = current->next;
delete temp;
}
head = NULL; // <============== code to add
Node* anotherCurrent = another.head;
while (anotherCurrent != NULL) {
*this += anotherCurrent->value;
anotherCurrent = anotherCurrent->next;
}
}
return *this;
顺便说一句,这是一个非常有趣的设计模式,必读:复制-交换习惯用法
相关文章:
- 项目错误:QT中的未知模块:Mac上的图表
- C++ Mac Xcode 错误:控制可能达到非无效功能的末尾
- Qt Debugger在Mac上使用错误的python版本
- 在 OpenGL 中在 Mac 上编译着色器时出现"version not supported"错误
- 在 Mac OS x 中制造期间出现错误未知类型名称"constexpr"
- 在 mac 上使用 MPFR 包编译错误
- MAC OpenGL 着色器错误:"version '150' is not supported "
- "cin"错误地解析了Mac上的"double"输入
- 在 Mac 上C++编译代码时处理“dyld:惰性符号绑定失败:找不到符号”错误
- OpenGL符号突出显示visual studio Mac代码中的错误
- 从Mac上的终端编译时C 错误
- CMAKE错误:C 11不支持Mac High Sierra
- Mac 上的 node-gyp 构建错误"calling a protected constructor of class 'v8::HandleScope'"
- 如何解决错误:在Mac上令牌"("之前缺少二进制运算符?
- 标准::字符串::空的未定义符号错误;Mac OS High Sierra 上的 c++ 标准方法链接错误
- OpenCV3.2 编译错误 MAC OS X
- QT Quick (QML) assemble mac-.bundle -> 不是使用 macdeployqt 时的动态库错误
- QT 5.12 Mac OS X版本号码错误
- XCode -Mac App -Bootstrap启动时检查错误
- RT 音频 Mac 错误 g++ 编译错误