当要求打印节点时,模板链接列表给出错误的输出

Templated Linked List giving wrong output when asked to print a node

本文关键字:列表 链接 出错 错误 输出 打印 节点      更新时间:2023-10-16

我正在为一个项目创建一个学校记录数据库。我有一个Student、Faculty和Administrator类,它们都继承了Person类的内容。当我将不同的对象添加到节点时,信息存储在该节点中(我通过调试器看到它),但是当我打印节点时,我会得到

00266A88

而不是

Full Name: Reed
M Number: 999
Email:

等等

我只是不确定是什么原因造成的问题。以下是我从列表中打印节点的方法:

template <typename T>
void TemplatedList<T>::printSpecific(int m_Number)
{
Node * Current = Head;
//If there is nothing in the list but the dummy head node, then return because there's nothing to print
if(Head->next == NULL)
{
    cout << "Cannot print (M" << m_Number << "), NOT found!" << endl;
    return;
}
else
    Current = Current->next;
// While Current->next isn't equal to NULL, go through the list and see if the M-Numbers match. If they do, print the student and return
while(Current->next != NULL)
{
    if(m_Number == Current->data->getM_Number())
    {
        cout << Current->data;
        return;
    }
    else
    {
        Current = Current->next;
    }
}
if(Current->next == NULL)
{
    if(m_Number == Current->data->getM_Number())
    {
        cout << Current->data;
        return;
    }
    else
    {
        cout << "Cannot print (M" <<m_Number << "), NOT found!" << endl;
        return;
    }
}
}

以下是将其中一个对象添加到列表的函数:

template<typename T>
void TemplatedList<T>::addTemplatedList(T newAddition)
{
//Points to current node we're using
Node* Current = Head;
//Points to the node previous in the list to the current
Node* Previous = Head;
//Creates a new Node
Node* newNode = new Node;
//Assigns new Student information to new Node
newNode->data = newAddition;
// Check to see if the Head is only thing in the list. If it is, just place the new Node directly after the Head
if (Head->next == NULL)
{
    Head->next = newNode;
    newNode->next = NULL;
    return;
}

else
{
    while (Current->next != NULL)
    {
        if (newAddition->getM_Number() < Current->next->data->getM_Number())
        {
            newNode->next = Current->next;
            Previous->next = newNode;
            return;
        }
        else if (newAddition->getM_Number() == Current->next->data->getM_Number())
        {
            cout << "Person with M Number " << newAddition->getM_Number() << " not added because they are already in database." << endl;
            delete newNode;
            return;
        }
        Current = Current->next;
        Previous = Previous->next;
    }
    if (Current->next == NULL)
    {
        Current->next = newNode;
        newNode->next = NULL;
    }
}
}

最后是我如何调用add函数并创建一个新对象:

if (inputArray[0] == "A")
    {
        cout << "Adding Administrator: " << endl <<"tFull Name:t" << inputArray[1] << endl;
        cout << "tM Number:t" << inputArray[2] << endl << "tEmail Addr:t" << inputArray[3] << endl << "tTitle:t " << inputArray[4] << endl;
        Administrator *newAdmin = new Administrator;
        istringstream stream (inputArray[2]);
        int number;
        stream >> number;
        newAdmin->setAdmin(inputArray, number);
        templatedList.addTemplatedList(newAdmin);
    }

我真的很感激并帮助我,因为我不确定发生了什么,也不确定为什么它会给我不正确的输出。

在本例中,Node::data似乎是指向Administrator的指针。所以当你做时

cout << Current->data;

它仅仅输出指针值。假设您已经为Administrator类实现了operator<<,那么您所需要做的就是取消引用:

cout << *Current->data;