简体   繁体   中英

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. 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" '.

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]. So map<char, type_node> is an error and consequently the alias is also an error.

In C++ all structs are implicitly 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?

  2. Difference between 'struct' and 'typedef struct' in C++?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM