【问题标题】:Threads on xcode while compiling the c++ code编译 c++ 代码时 xcode 上的线程
【发布时间】:2018-03-21 06:43:18
【问题描述】:

所以我试图使用双向链表在 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;

}

提前致谢。

【问题讨论】:

  • 您的复制赋值运算符正在手动调用析构函数..
  • 尝试使用复制构造函数。我想知道为什么每当我使用它时,我都会在 insertback 等函数上出现“THREAD 1= EXC_BAD_ACCESS (code=1, address=0x9)”之类的错误。
  • 当我什至没有在我的复制构造函数中调用析构函数时,它怎么能手动调用。?

标签: c++ xcode compiler-errors


【解决方案1】:

你的代码有很多问题..

您的节点构造函数没有正确委托..

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

    //....
}

【讨论】:

  • 我不敢相信我在复制构造函数上苦苦挣扎了这么久,而我所要做的就是添加两行代码。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-11-14
  • 1970-01-01
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多