模板参数需要向前声明,还是要知道类型定义

Mat*_*ser 1 c++

基本上我有这个代码,但是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:
    ...
Run Code Online (Sandbox Code Playgroud)

Cat*_*lus 7

所有标准容器都要求类型完整.由于您正在创建指针向量,这不是问题(从不完整类型派生的指针类型可以在那里用作类型参数).代码中的错误是= 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() {}
Run Code Online (Sandbox Code Playgroud)