简体   繁体   English

没有对象或结构的自引用typedef

[英]Self-referential typedef without object or struct

I'm fairly new to c++, and I'm trying to implement a simple trie to store a lexicon of strings, using the std::map or other c++ standard library container classes. 我对C ++还是很陌生,我正在尝试使用std :: map或其他c ++标准库容器类来实现一个简单的trie来存储字符串词典。 What I'd like to be able to do is use something like 我想做的是使用类似

typedef (map<char, type_node>)* type_node;

for which clang++ gives me the error ' use of undeclared identifier "type_node" '. 为此clang ++给我错误'使用未声明的标识符“ type_node”'。

This as opposed to a self-referential structure, such as 这与自我指称结构相反,例如

typedef struct node {
   int data;
   struct node *next;
} Node;

Which works fine, somehow. 哪个工作正常,以某种方式。

Is it possible to declare self-referential types without using a class or struct? 是否可以在不使用类或结构的情况下声明自引用类型?

Why does the structure work and not the recursive alias type? 为什么结构有效,而不是递归别名类型?

Is there a better way to do this? 有一个更好的方法吗?

You cannot declare containers with incomplete types[1]. 您不能声明类型不完整的容器[1]。 So map<char, type_node> is an error and consequently the alias is also an error. 所以map<char, type_node>是一个错误,因此别名也是一个错误。

In C++ all structs are implicitly typedef'd[2]. 在C ++中,所有结构都隐式为typedef'd [2]。 Your structure can be simplified to: 您的结构可以简化为:

struct node
{
   int data;
   node * next;
};

Links: 链接:

  1. Are C++ recursive type definitions possible, in particular can I put a vector<T> within the definition of T? 是否可以使用C ++递归类型定义,特别是我可以在T的定义中放入vector <T>吗?

  2. Difference between 'struct' and 'typedef struct' in C++? C ++中“ struct”和“ typedef struct”之间的区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何破坏包含自指指针的对象? - How to destruct object containing self-referential pointer? 自引用指针算法 - Self-referential pointer arithmetic 将类型参数传递给自引用指针 - Passing type parameter to self-referential pointer 如何在C ++中定义自引用映射? - How to define a self-referential map in C++ ? C ++中Tupper的自引用公式无法按预期工作? - Tupper’s Self-Referential Formula in C++ not working as expected? 如何在C ++中声明自引用容器? - How to declare a self-referential container in C++? 如何为具有自引用指针的类实现复制构造函数/赋值运算符? - How to implement a copy constructor / assignment operator for a class that has a self-referential pointer? 自引用使用unordered_map会导致gcc 5.3而不是clang出现问题 - Self-referential use of unordered_map causes problems for gcc 5.3 but not clang memory如何在C/C++中使用自引用结构指针的结构声明中工作? - How does memory work in declaration of structure which uses self-referential structure pointer in C/C++? 可以在 C++ 中实例化和/或专门化递归/自引用模板(使用指针)吗? - Can a recursive/self-referential template (using pointers) be instantiated and/or specialized in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM