简体   繁体   English

未声明类的类成员

[英]Class member of an undeclared class

How to make this bit of code work, if these two classes are declared in the same ".h" file? 如果这两个类在同一个“ .h”文件中声明,那么如何使这部分代码起作用? How to make it work if they are in the separate ones? 如果它们在单独的地方,如何使它起作用?

Simple question, but googling didn't help me. 简单的问题,但谷歌搜索并没有帮助我。

class Container
{
    Piece p;
public:
    Container() :p(this) {};
};

class Piece
{
    Container* cont;
public:
    Piece(Container * c) :cont(c) {};
};

Forward declare Container and then define it later on: 转发声明Container ,然后在以后定义它:

class Container;

class Piece
{
    Container* cont;
public:
    Piece(Container * c) :cont(c) {};
};

class Container
{
    Piece p;
public:
    Container() :p(this) {};
};

Forward declaration is what you are looking for. 前向声明是您要寻找的。

class Container;

class Piece
{
    Container* cont;
public:
    Piece(Container * c) :cont(c) {};
};
class Container
{
    Piece p;
public:
    Container() :p(this) {};
};

If you google you would find more information. 如果您使用google,则会找到更多信息。 See the one below. 请参阅下面的一个。

When can I use a forward declaration? 什么时候可以使用前向声明?

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

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