Search code examples
c++listcontainersnodesclass-template

I created a List Container class in C++ and it's not working as expected


So first I created a struct to contain the following:

template <class T>
struct ListNode 
{
    unsigned int id;

    T data;
    ListNode *next_left;
    ListNode *next_right;
};

And a class with the following:

template <class T>
class List 
{

public:

    unsigned int id_count;
    ListNode<T> *tail;

    List()
    { 
        tail = NULL;
        id_count = 0;
    }

    unsigned int add(T item)
    {
        id_count += 1;

        ListNode<T> *n = new ListNode<T>;
        n->id = id_count;
        n->data = item;
        n->next_left = tail;
        n->next_right = NULL;

        tail = n;
        if (n->next_left != NULL)
            n->next_left->next_right = n;

        return id_count;
    }

    ListNode<T> *getNode(unsigned int id)
    {
        bool found = false;
        ListNode<T> *np = tail;
        while(np != NULL)
        {
            if (np->id == id)
            {
                found = true;
                return np;
                break;
            }
            else
            {
                np = np->next_left;
            }
        }
        return NULL;
    }
};

Here is a link to the exact code of main.cpp: http://pastebin.com/fHhsvd9A

So in my main.cpp I create a List instance and a Node pointer:

List<F3D_model> GameEntities;
ListNode<F3D_model> *np;

Then I create two model class instances of the F3D_model Class and add them to the List intsance I created:

F3D_model model;
model.create();

F3D_model model2;
model2.create();

GameEntities.add(model);
GameEntities.add(model2);

Print models' information:

Created with ID: 1
Created with ID: 2

Model 1 Address: 07091F28
Model 2 Address: 070919D0

Model 1 Angle: 0
Model 2 Angle: 0

Tail ID: 2
Tail Data: 0
Tail Address: 070919D0

Code used to print information:

printf("Created with ID: %i\n",GameEntities.add(model));
printf("Created with ID: %i\n",GameEntities.add(model2));

printf("Model 1 Address: %p\n",GameEntities.getNode(1));
printf("Model 2 Address: %p\n",GameEntities.getNode(2));
printf("----------------\n");
printf("Model 1 Data: %i\n",GameEntities.getNode(1)->data.angle);
printf("Model 2 Data: %i\n",GameEntities.getNode(2)->data.angle);
printf("----------------\n");
printf("Tail ID: %i\n",(GameEntities.tail->id));
printf("Tail Data: %i\n",(GameEntities.tail->data.angle));
printf("Tail Address: %p\n",(GameEntities.tail));
printf("----------------\n");

( angle is a public var in F3D_model class )

Everything looks right. So I have a keypress event where I add a value to the models' angle.

GameEntities.getNode(1)->data.angle += 60;
GameEntities.getNode(2)->data.angle += 60;

and then it prints again the values.

So the problem is that when I press the key it prints the same, as if I hadn't added the values.

BUT when I press the key it does move the model on the screen, so it is actually adding the values. And when I change the type from F3D_model to int and do the same process, it works just fine.

SO my question is why isn't it working when I use the F3D_model, and how can I make it work?


Solution

  • From what you've described, it sounds like the issue is just how you're printing the angle:

    printf("Model 1 Data: %i\n",GameEntities.getNode(1)->data.angle);
    printf("Model 2 Data: %i\n",GameEntities.getNode(2)->data.angle);
    

    You've used %i which is an integer format specifier. You've said it prints what you expect when you change the definition of angle to be an int. If you change the format string to %f to print a float, or print static_cast<int>( GameEntities.getNode(1)->data.angle ) it should work.