Templates 程序停止运行?

Templates 程序停止运行?,templates,linked-list,stack,queue,Templates,Linked List,Stack,Queue,因此,出于某种原因,我的程序在第二次排队后停止运行。它编译并没有给出错误消息,只是突然退出。甚至连一条中止的消息都没有。我已经让另外两个比我更高的CS班的人看了一眼,他们也看不出任何错误 主要内容如下: using namespace std; #include <iostream> #include "Queue.h" #include "LinkedList.h" int main() { try { int type = 0; c

因此,出于某种原因,我的程序在第二次排队后停止运行。它编译并没有给出错误消息,只是突然退出。甚至连一条中止的消息都没有。我已经让另外两个比我更高的CS班的人看了一眼,他们也看不出任何错误

主要内容如下:

using namespace std;
#include <iostream>
#include "Queue.h"
#include "LinkedList.h"

int main()
{
  try
    {
        int type = 0;

        cout<<"What data type do you want to work with? 1 = int, 2 = char"<<endl;
        cin>>type;

        if(type == 1)
        {
            Queue<int> q;

            q.enqueue(1);
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.enqueue(5);
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.enqueue(3);
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.enqueue(5);
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.dequeue();
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.dequeue();
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.dequeue();
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;
        }

        if (type == 2)
        {
            Queue<char> q;

            q.enqueue('a');
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.enqueue('b');
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.enqueue('c');
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.dequeue();
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.dequeue();
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;

            q.dequeue();
            cout<<"The size is: "<< q.size() <<". And the top is: " << q.front() <<endl;
        }

        return 1;
    }
    catch (int e)
    {
        if (e == 2)
        {
            cout<<"Call to dequeue() generated an exception, because the queue is empty."<<endl;
        }
        else if (e == 3)
        {
            cout<<"Call to front() generated an exception, because the queue is empty."<<endl;
        }
    }
}
使用名称空间std;
#包括
#包括“Queue.h”
#包括“LinkedList.h”
int main()
{
尝试
{
int类型=0;

cout让我们看看在
LinkedList::insertback
中发生了什么,并在代码中添加一些注释:

template <class T>
void LinkedList<T>::insertAtBack(T valueToInsert)
{
    // Let's declare a pointer that is uninitialized and points to who 
    // knows where.
    Node<T>* newNode;

    // Now let's dereference this pointer. Remember, it points somewhere
    // random. Maybe this will crash, maybe this won't. It's undefined
    // behavior.
    newNode->val = valueToInsert;

    // We are well inside undefined behavior land now. I hope you brought
    // your passport and a fresh pair of pants.
    newNode->next = NULL;

    Node<T>* temp = first;

    if (temp != NULL) 
    {
        while (temp->next != NULL) 
        {
            temp = temp->next;
        }

        temp->next = newNode;
    }
    else 
    {
        // If we get here, first will be changed and end up pointing to
        // some random location in memory. And that's bad, mmkay?
        first = newNode;
    }
}
模板
void LinkedList::insertback(T valueToInsert)
{
//让我们声明一个未初始化的指针并指向谁
//知道在哪里。
节点*新节点;
//现在让我们去引用这个指针。记住,它指向某个地方
//随机的。也许这会崩溃,也许这不会。它没有定义
//行为。
newNode->val=valueToInsert;
//我们现在很好地进入了未定义的行为领域。我希望你带来了
//你的护照和一条新裤子。
newNode->next=NULL;
节点*温度=第一;
如果(温度!=NULL)
{
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
temp->next=newNode;
}
其他的
{
//如果我们到了这里,第一个将被改变,最后指向
//内存中的一些随机位置。这很糟糕,嗯?
第一个=新节点;
}
}

这个故事的寓意是,取消引用未初始化的指针是不好的

请不要在头文件中使用命名空间std;
。这是一种不好的做法。嗯,这实际上可以清除它。我以前有过节点temp=new Node();但不确定语法如何与模板一起工作,所以我只是更改了(虽然我当然知道它的价值)。我将尝试一下,看看它是否有效,谢谢!@DulceRios您需要
Node*newNode=newNode;
很高兴我能提供帮助。请注意,您的代码还有一些小问题,您当前的代码路径可能无法解决。请尝试使用纸和笔遍历两个
LinkedList
函数,执行每行代码就像一台懂
C++
的计算机。
#include <iostream>
#include "LinkedList.h"
#ifndef QUEUE_H
#define QUEUE_H

template <class T>
class Queue: public LinkedList<T>
{
  public:
        Queue();
        ~Queue();

        void enqueue(T value);
        T dequeue();
        T& front();
};

/**************CPP FILE*****************/

template <class T>
Queue<T>::Queue(){}

template <class T>      
Queue<T>::~Queue(){}

template <class T>      
void Queue<T>::enqueue(T value)
{
    LinkedList<T>::insertAtBack(value);
}

template <class T>  
T Queue<T>::dequeue()
{
    if(LinkedList<T>::isEmpty())
    {
        throw 2;
    }
    else
    {
        T firstElmnt = LinkedList<T>::firstNum();
        LinkedList<T>::removeFromFront();

        return firstElmnt;
    }
}

template <class T>  
T& Queue<T>::front()
{
    if(LinkedList<T>::isEmpty())
    {
        throw 3;
    }
    else
    {
        return LinkedList<T>::firstNum();
    }
}

#endif
template <class T>
void LinkedList<T>::insertAtBack(T valueToInsert)
{
    // Let's declare a pointer that is uninitialized and points to who 
    // knows where.
    Node<T>* newNode;

    // Now let's dereference this pointer. Remember, it points somewhere
    // random. Maybe this will crash, maybe this won't. It's undefined
    // behavior.
    newNode->val = valueToInsert;

    // We are well inside undefined behavior land now. I hope you brought
    // your passport and a fresh pair of pants.
    newNode->next = NULL;

    Node<T>* temp = first;

    if (temp != NULL) 
    {
        while (temp->next != NULL) 
        {
            temp = temp->next;
        }

        temp->next = newNode;
    }
    else 
    {
        // If we get here, first will be changed and end up pointing to
        // some random location in memory. And that's bad, mmkay?
        first = newNode;
    }
}