【问题标题】:C++ Struct - Error 1 error C2143: syntax error : missing ';' before '*'C++ Struct - 错误 1 ​​错误 C2143:语法错误:缺少 ';'前 '*'
【发布时间】:2013-10-09 08:27:53
【问题描述】:

当我尝试编译以下内容时,我收到错误“错误 1 ​​错误 C2143:语法错误:缺少 ';'前 '*'”。有谁知道我为什么会收到这个错误?我在这里做错了什么?

struct HE_edge {
HE_vert* vert; // vertex at the end of the half-edge<br>
HE_edge* pair; // oppositely oriented half-edge<br>
HE_face* face; // the incident face<br>
HE_edge* prev; // previous half-edge around the face<br>
HE_edge* next; // next half-edge around the face<br>
};

struct HE_vert {
float x, y, z; // the vertex coordinates<br>
HE_edge* edge; // one of the half-edges emanating from the vertex<br>
};

struct HE_face {
HE_edge* edge; // one of the half-edges bordering the face<br>
};

【问题讨论】:

  • 您需要以相反的顺序进行声明。 HE_edge 尝试使用编译器还不知道的HE_vert
  • 何前向声明你的类型?所以 HE_vert 没有在 HE_edge 的定义中命名类型。很多编译器现在告诉你这个,显然不是你的。
  • 事实上,我并没有立即意识到你有一个循环依赖。看forward declarations

标签: c++ struct


【解决方案1】:

尝试以正确的顺序声明你的结构: 由于 HE_edge 依赖于 HE_vert 和 HE_face,请在之前声明它们。

struct HE_vert;
struct HE_face;

struct HE_edge {
HE_vert* vert; // vertex at the end of the half-edge<br>
HE_edge* pair; // oppositely oriented half-edge<br>
HE_face* face; // the incident face<br>
HE_edge* prev; // previous half-edge around the face<br>
HE_edge* next; // next half-edge around the face<br>
};

struct HE_vert {
float x, y, z; // the vertex coordinates<br>
HE_edge* edge; // one of the half-edges emanating from the vertex<br>
};

struct HE_face {
HE_edge* edge; // one of the half-edges bordering the face<br>
};

【讨论】:

    【解决方案2】:

    HE_edge 中使用它们之前,您需要转发声明HE_vertHE_face

    // fwd declarations. Can use "struct" or "class" interchangeably.
    struct HE_vert;
    struct HE_face;
    
    struct HE_edge { /* as before */ };
    

    When to use forward declaration?

    【讨论】:

      【解决方案3】:

      在使用它们的类型来制作指针之前,你需要声明所有的类:

      struct HE_edge;
      struct HE_vert;
      struct HE_face;
      
      // your code
      

      【讨论】:

        【解决方案4】:

        您必须在使用标识符之前声明标识符。对于结构,这只需通过例如

        struct HE_vert;
        

        把它放在HE_edge的定义之前。

        【讨论】:

          【解决方案5】:

          第一个声明不知道HE_vertHE_face 是什么,你需要告诉编译器它们是什么:

          struct HE_face;
          struct HE_vert;//tell compiler what are HE_vert and HE_face
          
          struct HE_edge {
          HE_vert* vert; // vertex at the end of the half-edge<br>
          HE_edge* pair; // oppositely oriented half-edge<br>
          HE_face* face; // the incident face<br>
          HE_edge* prev; // previous half-edge around the face<br>
          HE_edge* next; // next half-edge around the face<br>
          };
          
          struct HE_vert {
          float x, y, z; // the vertex coordinates<br>
          HE_edge* edge; // one of the half-edges emanating from the vertex<br>
          };
          
          struct HE_face {
          HE_edge* edge; // one of the half-edges bordering the face<br>
          };
          

          拉兹万。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-05-17
            • 1970-01-01
            • 2013-07-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多