模板参数需要正向声明,或者类型定义需要已知

Do template parameters need to be forward declared or does the type definition to be known

本文关键字:类型 或者 定义 参数 声明      更新时间:2023-10-16

基本上我有这个代码,但GCC抱怨矢量不能用空类型构造。有人遇到这个问题吗之前我应该提到的是,Vertex3D仅通过文件,因此不应该有理由将整个类型编译器已知。我不知道模板在这方面的表现尊敬

//#include "cgVertex3D.hpp"                                                                                                                                                                                              
#include "cgDirection3D.hpp"
#include "cgHandedness.hpp"
class Vertex3D; // Forward declaration to avoid mutual header include
class Polygon3D {
  // Vertices constituting this polygon
  vector<Vertex3D*> *vertices = NULL;
  public:
    ...

所有标准容器都要求类型完整。由于您正在创建一个指针向量,所以这不是问题(从不完整类型派生的指针类型可以用作类型参数)。代码中的错误是= NULL;部分(好吧,除非std::vector本身不可用,否则您还必须包含<vector>),这无论如何都没有什么意义。您在构造函数中初始化数据成员,而不是在类定义体中初始化。而且你绝对不需要有一个指向向量的指针。

// note that you probably *shouldn't* use pointers here, but whatever
class Polygon3D {
    std::vector<Vertex3D*> vertices;
    Polygon3D();
    // ...
};
// ...
Polygon3D::Polygon3D() : vertices() {}
相关文章: