Kal*_*lec 0 c++ templates struct
我是编程和C++的初学者.我之前使用过模板,但是以非常有限的方式使用模板,我不知道我做错了什么:
template <typename TElement>
struct list{ // if I try list<TElement> => list is not a template
TElement data;
struct list *next;
} node_type; // also tried node_type<TElement>, other incomprehensible errors
node_type *ptr[max], *root[max], *temp[max];
Run Code Online (Sandbox Code Playgroud)
我发现错误有点难以理解:"node_type does not name a type"
我做错了什么?
我打算做什么:
我想要一个类型列表(所以我可以在几个完全不同的抽象数据类型 - ADT上使用它),所以我想最终得到这样的东西:
Activities list *ptr[max], *root[max], *temp[max]
Run Code Online (Sandbox Code Playgroud)
如果这有意义(在哪里Activities是一个类,但可以是任何其他类).
试试这个:
template <typename TElement>
struct node{
TElement data;
node* next;
};
node<int>* ptr[max], *root[max], *temp[max];
Run Code Online (Sandbox Code Playgroud)
一个额外的建议:避免C++标准库类型(如后命名您的自定义类型list,vector,queue...这些都是命名空间std).这是令人困惑的,并且可能导致名称冲突(除非您将它放在您自己的命名空间中,无论您放在哪里都需要明确地使用它using namespace std;).
| 归档时间: |
|
| 查看次数: |
114 次 |
| 最近记录: |