悠悠u 2022-09-15 14:31 采纳率: 100%
浏览 101
已结题

引发了未经处理的异常:读取访问权限冲突。 **this** 是 nullptr。

 ```c++
#include <stdio.h>
#include <malloc.h>
#include<iostream>
using namespace std;
template<class ElemType>
struct Node
{
    ElemType data;//数据
    struct Node<ElemType>* next;//后继指针
};
template<class ElemType>
class Queue
{
public:
    Node<ElemType>* tail;//尾指针
public:
    Queue();
    bool InQueue(ElemType x);
    bool OutQueue();
    bool GetHead();
};
template<class ElemType>
Queue<ElemType>::Queue()
{
    Queue<ElemType>* queue = new Queue<ElemType>;
        //malloc(sizeof(Queue));
    Node<ElemType>* node = new Node<ElemType>;
        //malloc(sizeof(Node));
    queue->tail = node;
    queue->tail->nex = queue->tail;//指向自己
    return queue;
}
template<class ElemType>
bool Queue<ElemType>::InQueue(ElemType x)
{
    
    Node<ElemType>* p = new Node<ElemType>;
        //malloc(sizeof(Node<ElemType>));
    p->data = x;
    p->next = this->tail->next;
    this->tail->next = p;
    this->tail = p;
    return true;
}
template<class ElemType>
bool Queue<ElemType>::OutQueue()
{
    Node<ElemType>* node;
    node = this->tail->next->next;
    if (this->tail->next->next == this->tail->next)
    {
        cout << "队列为空" << endl;
        return false;//判断为空
    }
    ElemType x = node->data;
    if (node == this->tail)//只有一个结点
    {
        this->tail = this->tail->next;
        this->tail->next = this->tail;
    }
    else
        this->tail->next->next = node->next;
    free(node);//清除数据
    cout << "删除数据为:" << x << endl;
    return true;

}
template<class ElemType>
bool Queue<ElemType>::GetHead()
{
    Node<ElemType>* node;
    node = this->tail->next->next;
    if (this->tail->next->next == this->tail->next)
    {
        cout << "队列为空" << endl;
        return false;//判断为空
    }
    ElemType x = node->data;
    cout << "队头数据为" << x << endl;
    return true;
}
int main()
{
    Queue<int>* queue=NULL;
    queue->InQueue(1);
    queue->OutQueue();
    queue->GetHead();

}



![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/743944322366142.jpg "#left")

  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-09-15 14:54
    关注
    Queue<int>* queue=NULL;
        queue->InQueue(1);
    
    

    这一看就不对啊,queue等于NULL了,怎么能进行指针操作呢???不要用指针

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 8月6日
  • 已采纳回答 7月30日
  • 创建了问题 9月15日

悬赏问题

  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题
  • ¥15 vs2019的js智能提示
  • ¥15 关于#开发语言#的问题:FDTD建模问题图中代码没有报错,但是模型却变透明了
  • ¥15 uniapp的h5项目写一个抽奖动画