c++ - 如何递归使用typedef?

标签 c++ c++11 typedef

我想编写一个关于一个人每学期可以选修的类(class)的程序。我想定义一个名为 CourseTable 的数据类型,它是一个名为 Course 的结构体的 vector ,但在这个结构体中我还有一个“先决条件”变量,其类型我决定成为 CourseTable。

typedef vector<Course> CourseTable ;
struct Course
{
  ....
  CourseTable prerequisites;
};

无论我放置这两个声明的顺序如何,我都会遇到编译错误。您对我应该做什么有什么建议吗?

最佳答案

尝试在结构中放置别名:

struct Course {
    typedef std::vector<Course> CourseTable;
    CourseTable prerequisites;
};

但是,更多 C++ 风格的解决方案是使用 using类型别名的关键字:

struct Course {
    using CourseTable = std::vector<Course>;
    CourseTable prerequisites;
};

或转发声明struct Course,例如:

struct Course;
using CourseTable = std::vector<Course>;

struct Course {
    CourseTable prerequisites;
};

关于c++ - 如何递归使用typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60527986/

相关文章:

c++ - Asio 内部线程对用户透明吗?

c++ - 继承构造函数后查找基类名

c++ - 内部 typedef 和循环依赖

c++ - 为什么 CFile + CArchive 比 C I/O 流性能更好?

c++ - 将 shared_ptr 参数添加到容器的有效方法?

c++ - clang++、boost::spirit 和 c++11

部分模板的 C++ typedef

c - 局部同义变量到非精确类型

C++ 选择文件夹,包含文件

c++ - 从 Eigen::Map 构造 Eigen::Array,它是如何工作的?