gpt4 book ai didi

c++ - 编译 c++ 代码时 xcode 上的线程

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:07:59 25 4
gpt4 key购买 nike

所以我尝试使用双向链表在双端队列上构建这个项目。但是当我构建它时。它说构建但提供线程并且不提供所需的输出。我一次又一次地重新实现了主要问题(复制构造函数)和所有函数,但它仍然每次都给我新线程。

这是头文件。

#pragma once
#include <stdexcept>
using namespace std;

class Node
{
public:

int data;
Node* next;
Node* previous;
Node();
Node(const int &x);

};

class Deque
{
public:

Deque();
Deque(const Deque &d);
Deque &operator= (const Deque &d);
~Deque();
void insertFront(const int &x);
void insertBack(const int &x);
int removeFront();
int removeBack();
int peekFront();
int peekBack();
bool empty() const;
int size()const;
friend ostream& operator << (ostream &out, const Deque &d);


private:

Node* front;
Node* rear;

};

这将是 .cpp(实现文件。)

//
// Deque_cmpt225.cpp
// Deque_cmpt225
//
// Created by Aryan Arora on 2017-10-09.
// Copyright © 2017 Aryan Arora. All rights reserved.
//

#include "Deque_cmpt225.h"
#include <iostream>
#include <stdexcept>
using namespace std;

Node:: Node()
{
previous = nullptr;
next = nullptr;
data = 0;
}

Node:: Node(const int &x)
{
Node();
data = x;
}


Deque:: Deque() //Empty Deque.
{
front = nullptr;
rear = nullptr;

}

Deque:: ~Deque()
{
if (this->empty())
return;
else{
Node* temp;
while (this->front->next != nullptr){
temp = this->front;
this->front = this->front->next;
delete temp;

}
temp = this->front;
this->front = nullptr;
this->rear = nullptr;
delete temp;

}


}


Deque:: Deque (const Deque &d) //Copy Constructor
{
if (d.empty()) //Deque is empty.
{
return;
}
Node* temp = d.front;
int x;
if (temp->next == nullptr) //Deque of just one node
{
x = temp->data;
Node *n1 = new Node (x);
n1->next = nullptr;
n1->previous = nullptr;
this->front = n1;
this->rear = n1;

}
else //Deque has more than one node
{
while (temp!= nullptr)
{
this->insertBack(temp->data);
temp = temp -> next;
}


}

}



Deque& Deque:: operator=(const Deque &d) //============================check again
{
if (this == &d)
return *this;
else
{
this->~Deque(); //DELETING THE DEQUE
Node* temp = d.front; //COPYING EACH NODE
while (temp != NULL)
{
this->insertBack(temp->data); //INSERTING AT THE BACK
temp = temp->next; //POINTING TEMP TO NEXT NODE
}

}
return *this;


}

void Deque:: insertFront(const int &x)
{

Node* temp = new Node(x);
temp->next = nullptr;
temp->previous = nullptr;
if (empty())
{
this->front = temp;
this->rear = temp;
}
else
{
temp->next = this->front;
temp->previous = nullptr;
this->front->previous = temp;
this->front = temp;
}

}

void Deque:: insertBack(const int &x)
{
Node* temp = new Node(x);
temp->next = nullptr;
temp->previous = nullptr;
if (empty())
{
this->front = temp;
this->rear = temp;
}
else
{
temp->next = nullptr;
temp->previous = this->rear;
this->rear->next = temp;
this->rear = temp;

}

}

int Deque:: removeFront()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else{
Node* temp;
temp = this->front;
int x = temp->data;

if ( this->front->next != nullptr )
{
this->front = this->front->next;
this->front->previous = nullptr;
}
else
{
this->front = nullptr;
this->rear = nullptr;
}
delete temp;

return x;
}

}


int Deque:: removeBack()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");

}
else{
Node* temp = this->rear;
int x = temp->data;

if ( this->rear->previous != nullptr )
{
this->rear = this->rear->previous;
this->rear->next = nullptr;
}
else
{
this->rear = nullptr;
this->front = nullptr;
}
delete temp;

return x;
}

}

int Deque:: peekFront()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else
{
return this->front->data;
}

}
int Deque:: peekBack()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else
{
return this->rear->data;
}
}

bool Deque:: empty() const
{
if (this->front == nullptr && this->rear == nullptr)
return true;
else
return false;
}


int Deque:: size() const
{
Node* temp = this->front;
int count = 0;
while (temp != nullptr)
{
count++;
temp = temp->next;
}
return count;

}

ostream& operator << (ostream &out, const Deque &d)
{
Node* temp = d.front;
out << "NULL -> ";
while (temp != nullptr)
{
out << temp->data << " <-> ";
temp= temp->next;


}

out << "<- NULL" << endl;
return out;

}

提前致谢。

最佳答案

你的代码有很多问题..

您的节点构造函数未正确委托(delegate)..

Node::Node() 
{
previous = nullptr;
next = nullptr;
data = 0;
}

Node::Node(const int &x)
{
Node(); //Creates a temporary node that gets destroyed immediately..
data = x;
}

如果改成这样就简单多了:

Node::Node() : Node(0) //Delegating constructor.
{
}

Node::Node(const int &x) : previous(nullptr), next(nullptr), data(x)
{
}

这一点不是真正的问题,但值得一提。您在构造后立即将 Node 指针设置为 nullptr。这不是必需的,因为您的构造函数已经这样做了..

Node* temp = new Node(x);
temp->next = nullptr; //Not needed anymore with the above fixes.
temp->previous = nullptr; //Not needed anymore with the above fixes.

您永远不会在复制构造函数中初始化您的变量。在你的构造函数中,你有(我改变了它,但它具有相同的含义):

Deque::Deque() : front(nullptr), rear(nullptr)
{
}

但是在你的复制构造函数中,你有:

Deque::Deque(const Deque &d)
{
//Other code here.. You never initialized front and rear to nullptr..
}

您永远不会将 frontrear 设置为 nullptr 所以 empty() 返回 false 因为它们是 "random"未初始化的值。然后在 insertBack 中你继续访问这个和 boom.. 访问冲突。

要修复它,您需要:

Deque::Deque(const Deque &d) : front(nullptr), rear(nullptr)
{
//Other code here..
}

下一个问题是您的复制赋值运算符正在调用析构函数!

Deque& Deque::operator=(const Deque &d)
{
if (this == &d)
return *this;
else
{
this->~Deque() //YOU CANNOT DO THIS.. Create a private member function for cleaning up.. Then call that function in your destructor and call that function here.. You cannot invoke the destructor like this.
}

//....
}

关于c++ - 编译 c++ 代码时 xcode 上的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46656900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com