循环包含问题与扩展类c ++

你能想出如何解决这个循环包含问题吗?

C延伸B和B包括A. A包括C.我试过的每个前向声明都没有奏效。

错误

错误1错误C2504:'B':基类未定义错误2错误C3668:'C :: c':带覆盖说明符'override'的方法未覆盖任何基类方法

档案啊:

#pragma once

#include "C.h"

struct A
{
    A();

    C c;
};

文件Bh:

#pragma once

#include "A.h"

struct A;

struct B
{
    virtual void c() = 0;

    A* a;
};

文件Ch:

#pragma once

#include "B.h"

struct B;

struct C : public B
{
    void c() override;
};

解决方案总是相同的,它看起来像你在正确的轨道上。 但是,您没有正确使用前向声明。 这里应该有很多例子来说明如何做到这一点。

BH

// Why are including "A.h" here, this is causing your circular include
// issue, it needs to be moved to your implementation (e.g., "B.cpp")
#include "A.h"

struct A; // Forward declaration, good

struct B
{
    virtual void c() = 0;

    A* a; // A pointer only requires the above forward declartion
};

#include "B.h" // Necessary since you're extending `B`

struct B; // This forward declaration is superfluous and should be removed

struct C : public B
{
    void c() override;
};
链接地址: http://www.djcxy.com/p/95537.html

上一篇: Circular inclusion issue with extended class c++

下一篇: C++ error Undefined reference to Class::Function()