0

我正在为一个项目的学校记录创建一个数据库。我有一个 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);
    }

我真的很感激我能得到帮助,因为我只是不确定发生了什么或者为什么它给了我不正确的输出。

4

1 回答 1

3

在这个例子中,它看起来像是Node::data一个指针。Administrator所以当你这样做时

cout << Current->data;

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

cout << *Current->data;
于 2013-02-21T02:03:11.353 回答