【问题标题】:Access "public" struct defined inside a class访问在类中定义的“公共”结构
【发布时间】:2019-03-28 18:28:41
【问题描述】:

我正在尝试创建一个类,其私有成员必须访问在同一类中使用公共访问定义的结构。我正在使用 VS Code 编写代码。当我尝试编写私有成员函数时,它说结构标识符未定义。

class Planner
{
  private:
    typedef std::pair<int, int> location;
    std::vector<location> obstacles;
    Pose next_state(const Pose& current_state, const Command& command);

  public:
    Planner(/* args */);
    virtual ~Planner();

    /* data */
    struct Command
    {
        unsigned char direction;
        unsigned char steering;
    };

    struct Pose
    {
        int x;
        int y;
        int theta;
    };

    struct Node
    {
        Pose pose;
        int f;
        int g;
        int h;
    };
};

在这里,它说'标识符“姿势”未定义'。我想了解这里发生了什么。

【问题讨论】:

    标签: c++


    【解决方案1】:

    在这里,它说'标识符“姿势”未定义'。我想了解这里发生了什么。

    那是因为您在编译器可以在 private 部分看到它们之前引入了 PoseCommand 类型引用:

    private:
        // ...
        Pose next_state(const Pose& current_state, const Command& command);
                           // ^^^^                       ^^^^^^^
    

    编译器需要在使用前查看标识符


    解决这个问题的方法是您需要在 Planner 类中正确排序前向声明:

    class Planner {
      // <region> The following stuff in the public access section,
      // otherwise an error about "redeclared with different access" will occur.
      public:
        struct Pose;
        struct Command;
      // </region> 
    
      private:
        typedef std::pair<int, int> location;
        std::vector<location> obstacles;
        Pose next_state(const Pose& current_state, const Command& command);
    
      public:
        Planner(/* args */);
        virtual ~Planner();
    
        /* data */
        struct Command {
            unsigned char direction;
            unsigned char steering;
        };
    
        struct Pose {
            int x;
            int y;
            int theta;
        };
    
        struct Node {
            Pose pose;
            int f;
            int g;
            int h;
        };
    };
    

    请参阅working code

    另一种方法是重新排列publicprivate 部分1,如@2785528's answer 中所述。


    1)请注意,这些可以在类声明中多次提供。

    【讨论】:

      【解决方案2】:

      还请考虑:您可以稍微重新排列代码而不添加行。

      class Planner
      {
      private:
         typedef std::pair<int, int> location;
         std::vector<location> obstacles;
         // "next_state" private method moved below
      
      public:
         Planner(/* args */){};
         virtual ~Planner(){};
      
         /* data */
         struct Command
         {
            unsigned char direction;
            unsigned char steering;
         };
      
         struct Pose
         {
            int x;
            int y;
            int theta;
         };
      
         struct Node
         {
            Pose pose;
            int f;
            int g;
            int h;
         };
      
      private:
         Pose next_state(const Pose& current_state, const Command& command);
      
      };
      

      您可能有多个私人部分。

      此外,您可以考虑在类声明的末尾将所有私有属性一起移动。

      【讨论】:

      • 当然,反过来用重新排列代替前向声明也可以。不错的选择。
      【解决方案3】:

      文件按顺序解析。在定义姿势之前,您正在引用它。您可以使用成员函数和变量来执行此操作,但这些是例外而不是规则。

      在您的情况下,解决此问题的一种简单方法是将私有部分移到末尾。

      【讨论】:

      • 旁注:类内的函数定义可以绕过排序并使用类中稍后声明的成员。
      • @user4581301 我想我不明白你的评论。是什么让这个答案是错误的?
      • 我自己对此感到困惑。太简单?提前声明在意识形态上更正确?没有线索。我只是想指出,一切都按顺序处理有一个例外,因为大多数时候你说的都是正确的。
      • 这个答案的第一个版本不好(-1)。当前版本是平庸的,但我们实际上不需要它,因为我们现在有很好的答案。
      • @anatolyg:其他答案都没有试图解释问题所在。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多