简体   繁体   English

如何避免前向声明错误?

[英]How can I avoid forward declaration error?

I got the following code in C++: 我在C ++中得到以下代码:

class Level;

class Node
{
  char letter;
  std::string path[2];
  Node *next;
  Level *nlevel;

  public:

    Node()
    {
      path[0] = "";
      path[1] = "";
      next = NULL;
      nlevel = NULL;
    }

    Node(char l, unsigned int h)
    {
      letter  = l;
      path[0] = "";
      path[1] = "";
      next = NULL;
      nlevel = NULL;
      nlevel->height = h;
    }

    virtual ~Node();
};

class Level
{
  std::list<Node> vnodes;
  unsigned int height;

  public:

    Level();
    virtual ~Level();
};

What is the correct way to call or declare the classes? 调用或声明类的正确方法是什么? I have been reading this and I already tried putting class Level; 我一直在阅读此书,并且已经尝试过上class Level; before class Node but I got a forward declaration error and if I separate each class in a different file to later include it I will got an error anyway since they depends each other, so how should a declare them? 在Node类之前,但是我收到一个前向声明错误,如果我将每个类都放在一个不同的文件中,以后再包含它,由于它们彼此依赖,无论如何我都会遇到一个错误,那么应该如何声明它们呢?

You can forward declare only if you use a pointer of the forward declared class. 仅当使用正向声明的类的指针时,才能向前声明。 Since you are using a member of Level at nlevel->height = h; 由于您正在使用Level的成员, Levelnlevel->height = h; you have to change the definition order of the classes. 您必须更改类的定义顺序。 That will work, because Level contains just a pointer to Node . 这将起作用,因为Level仅包含一个指向Node的指针。

Because height is a private member of Level you also have to add friend class Node; 因为heightLevel的私有成员,所以您还必须添加friend class Node; to class Level . 上课Level

   class Node;
   class Level
   {
       friend class Node;
       std::list<Node> vnodes;
       unsigned int height;

       public:

       Level();
       virtual ~Level();
   };

   class Node
   {
       char letter;
       std::string path[2];
       Node *next;
       Level *nlevel;

       public:

       Node()
       {   
           path[0] = ""; 
           path[1] = ""; 
           next = NULL;
           nlevel = NULL;
       }   

       Node(char l, unsigned int h)
       {
           letter  = l;
           path[0] = "";
           path[1] = "";
           next = NULL;
           nlevel = NULL;
           nlevel->height = h;
       }

       virtual ~Node();
   };

The way to solve this problem is to put the function definitions for Node after the class definition of Level so the compiler has its complete type description available: 解决这个问题的方法是把函数定义为Node的类定义 Level ,因此编译器具有完整的类型描述可供选择:

class Level;

class Node
{
  char letter;
  std::string path[2];
  Node *next;
  Level *nlevel;

  public:

    Node(); // put the definition after

    Node(char l, unsigned int h);

    virtual ~Node();
};

class Level
{
  std::list<Node> vnodes;
  unsigned int height;

  public:

    Level();
    virtual ~Level();
};

// put node's function definitions AFTER the definition of Level
Node::Node()
{
  path[0] = "";
  path[1] = "";
  next = NULL;
  nlevel = NULL;
}

Node::Node(char l, unsigned int h)
{
  letter  = l;
  path[0] = "";
  path[1] = "";
  next = NULL;
  nlevel = NULL;
  nlevel->height = h; // Now you have access problem
}

Or you can move the function definitions into a separate .cpp source file. 或者,您可以将函数定义移到单独的.cpp源文件中。

Now you have a new problem, nlevel->height = h; 现在您有一个新问题, nlevel->height = h; is trying to access a private member of Level . 正在尝试访问Level私人成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM