【问题标题】:C++ error that I do not understand: syntax missing ';' before identifier我不明白的 C++ 错误:语法缺失 ';'在标识符之前
【发布时间】:2013-02-12 17:22:08
【问题描述】:
class Dialogue
{
public:
    int id;
    int trigger;
    Question descendants[5]; // Max questions per dialogue
    string text;
};

class Question
{
public:
    int id;
    int descendant;
    int ancestor;
    string text;
};

当我尝试构建它时,它对问题后代位显示以下错误?:

错误 1 ​​错误 C2146:语法错误:缺少 ';'在标识符之前 '后代' c:\users**\documents\visual studio 2012\projects\game\game\dialogue.h 8 1 游戏错误 2 错误 C4430: 缺少类型说明符 - 假定为 int。注意:C++ 不支持 默认整数 c:\users**\documents\visual studio 2012\projects\game\game\dialogue.h 8 1 游戏

【问题讨论】:

  • 前向声明?类声明的交换顺序
  • 我会说这是因为Question是在Dialogue之后定义的,但是在Dialogue的定义中使用。
  • 在将Question 声明为类之前,您正在使用它。在这种情况下,您可以将Question 放在Dialogue 之上,但有时您需要一个前向声明[class Question;] 来告诉编译器有一个该名称的类。

标签: c++ class identifier


【解决方案1】:

或者你可以转发声明你的类。当它们都相互依赖时,这很方便:

class Question;
class Dialogue;

类对话 { 民众: 内部标识; 整数触发器; 问题后代[5]; // 每个对话的最大问题数 字符串文本; };

类问题 { 民众: 内部标识; 整数后代; int 祖先; 字符串文本; };

【讨论】:

    【解决方案2】:

    您需要切换定义,以便在您在Dialogue 的声明中使用Question 时编译器知道它。

    这将编译:

    class Question
    {
    public:
        int id;
        int descendant;
        int ancestor;
        string text;
    };
    
    class Dialogue
    {
    public:
        int id;
        int trigger;
        Question descendants[5]; // Max questions per dialogue
        string text;
    };
    

    【讨论】:

      【解决方案3】:

      Question类的定义应该在前面,然后是Dialogue类。

      class Question
      {
      public:
          int id;
          int descendant;
          int ancestor;
          string text;
      };
      
      class Dialogue
      {
      public:
          int id;
          int trigger;
          Question descendants[5]; // Max questions per dialogue
          string text;
      };
      

      【讨论】:

      • 啊,伙计,我不知道你必须按顺序声明类。说得通。谢谢。我和我的菜鸟^_^
      【解决方案4】:

      类声明的交换顺序:

      class Question
      {
      public:
          int id;
          int descendant;
          int ancestor;
          string text;
      };
      
      class Dialogue
      {
      public:
          int id;
          int trigger;
          Question descendants[5]; // Max questions per dialogue
          string text;
      };
      

      【讨论】:

        猜你喜欢
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多