Circular inclusion issue with extended class c++

Can you figure out how to solve this circular inclusion problem?

C extends B and B includes A. A includes C. Every forward declaration I tried hasn't worked.

Errors

Error 1 error C2504: 'B' : base class undefined Error 2 error C3668: 'C::c' : method with override specifier 'override' did not override any base class methods

File Ah:

#pragma once

#include "C.h"

struct A
{
    A();

    C c;
};

File Bh:

#pragma once

#include "A.h"

struct A;

struct B
{
    virtual void c() = 0;

    A* a;
};

File Ch:

#pragma once

#include "B.h"

struct B;

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

The solution is always the same and it looks like you were on the right track. However, you're not using forward declartions correctly. There should be many examples of how to do this for example here.

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

Ch

#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/95538.html

上一篇: Ogre3D显示段错误

下一篇: 循环包含问题与扩展类c ++