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

时间:2017-10-10 00:25:23

标签: c++ xcode compiler-errors

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

这是头文件。

#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;

}

提前感谢。

1 个答案:

答案 0 :(得分:1)

你的代码有很多问题..

你的Node构造函数没有正确委托..

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,因为它们是“随机”未初始化的值。然后在insertBack中继续访问这个和繁荣..访问违规。

要解决此问题,请执行以下操作:

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.
    }

    //....
}