rsvar67
rsvar67

Reputation: 77

Access "public" struct defined inside a class

I am trying to make a class whose private member has to access a struct defined with public access in the same class. I am using VS Code to write the code. When I try to write a private member function, it says the struct identifier is not defined.

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;
    };
};

Here, it says 'identifier "Pose" is undefined'. I would like to understand what is going on here.

Upvotes: 2

Views: 591

Answers (3)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

Here, it says 'identifier "Pose" is undefined'. I would like to understand what is going on here.

That's because you introduced Pose and Command type references before the compiler could see them in the private section:

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

The compiler needs to see identifiers before their usage.


The way to solve that is you need properly ordered forward declarations within your Planner class:

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;
    };
};

See the working code.

The alternative is to rearrange your public and private sections1 as mentioned in @2785528's answer.


1)Note these can be provided multipe times within a class declaration.

Upvotes: 4

2785528
2785528

Reputation: 5566

Consider also: you may slightly re-arrange your code without adding lines.

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);

};

You may have more than one private section.

Also, you might consider moving all the private attributes together at the end of class declaration.

Upvotes: 2

Jack Aidley
Jack Aidley

Reputation: 20107

Files are parsed in order. You are referencing Pose before you are defining it. You can do this with member functions and variables but those are the exception not the rule.

An easy way to solve this in your case is to move the private section to the end.

Upvotes: 0

Related Questions