cadavid4j
cadavid4j

Reputation: 171

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

I'm creating a database for school records for a project. I have a Student, Faculty, and Administrator class that all inherit things from a Person class. When I add the different objects to a Node, the information is stored in that Node (I see it via the debugger), however when I go to print a Node, I get

00266A88

instead of

Full Name: Reed
M Number: 999
Email:

and so on.

I'm just not sure what is causing the problem. Here is my method to print a node from the list:

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

}

Here is the function to add one of the of the objects to the list:

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

}

And finally here is how I'm calling the add function and creating a new object:

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);
    }

I would really appreciate and help that I can get because I'm just not sure what's happening or why it's giving me that incorrect output.

Upvotes: 0

Views: 97

Answers (1)

zdan
zdan

Reputation: 29450

It looks like Node::data is a pointer to Administrator in this example. So when you do

cout << Current->data;

it merely outputs the pointer value. Assuming that you have implemented operator<< for the Administrator class, all you need to do is dereference:

cout << *Current->data;

Upvotes: 3

Related Questions