所以首先我创建了一个结构来包含以下内容:
template <class T>
struct ListNode
{
unsigned int id;
T data;
ListNode *next_left;
ListNode *next_right;
};
还有一个具有以下内容的类:
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;
}
};
这是 main.cpp 的确切代码的链接:http: //pastebin.com/fHhsvd9A
所以在我的 main.cpp 中,我创建了一个 List 实例和一个 Node 指针:
List<F3D_model> GameEntities;
ListNode<F3D_model> *np;
然后我创建 F3D_model 类的两个模型类实例,并将它们添加到我创建的 List 实例中:
F3D_model model;
model.create();
F3D_model model2;
model2.create();
GameEntities.add(model);
GameEntities.add(model2);
打印模型信息:
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
用于打印信息的代码:
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");
(角度是 F3D_model 类中的公共变量)
一切看起来都不错。所以我有一个按键事件,我为模型的角度添加了一个值。
GameEntities.getNode(1)->data.angle += 60;
GameEntities.getNode(2)->data.angle += 60;
然后它再次打印值。
所以问题是,当我按下键时,它的打印结果是一样的,就好像我没有添加值一样。
但是当我按下它时它确实会在屏幕上移动模型,所以它实际上是在添加值。当我将类型从 F3D_model 更改为 int 并执行相同的过程时,它工作得很好。
所以我的问题是为什么当我使用 F3D_model 时它不起作用,我怎样才能让它起作用?