我在使用前向声明时遇到问题,而且我不知道如何解决它。这是我的文件:

BubblePlug.h

#ifndef _BUBBLEPLUG_
#define _BUBBLEPLUG_

#include "IPlug_include_in_plug_hdr.h"
#include "resource.h"
#include "IControl.h"

class IPianoRoll;
class IMidiEngine;

class BubblePlug: public IPlug
{
private:

public:
    IMidiEngine *pMidiEngine;
    IPianoRoll *pPianoRoll;

    BubblePlug(IPlugInstanceInfo instanceInfo);
    ~BubblePlug();
};

#endif // !_BUBBLEPLUG_

BubblePlug.cpp
#include "BubblePlug.h"
#include "IPlug_include_in_plug_src.h"
#include "IPianoRoll.h"
#include "IMidiEngine.h"

BubblePlug::BubblePlug(IPlugInstanceInfo instanceInfo) : IPLUG_CTOR(10, 1, instanceInfo) {
    pPianoRoll = new IPianoRoll(this, 8, 8);
    pMidiEngine = new IMidiEngine(this);
}
BubblePlug::~BubblePlug() {
    delete pPianoRoll;
    delete pMidiEngine;
}

IPianoRoll.h
#ifndef _IPIANOROLL_
#define _IPIANOROLL_

#include "IMidiEngine.h"

class IPianoRoll : public IControl
{
private:
    BubblePlug *pBubblePlug;

public:

    IPianoRoll(BubblePlug *bubbleplug, int x, int y) : IControl(bubbleplug, IRECT(x, y, x + 10, y + 10)), pBubblePlug(bubbleplug) {

    }
    ~IPianoRoll() {

    };

    bool Draw(IGraphics *pGraphics) {
        return true;
    }

    void Random(bool onlyScore = false) {
        pBubblePlug->pMidiEngine->Init();
    }

    void Start() {

    }
};

#endif // !_IPIANOROLL_

IMidiEngine.h
#ifndef _IMIDIENGINE_
#define _IMIDIENGINE_

class IMidiEngine
{
private:
    BubblePlug *pBubblePlug;

public:
    IMidiEngine(BubblePlug *bubbleplug) : pBubblePlug(bubbleplug) {

    }
    ~IMidiEngine() {

    };

    void Init(bool randomScore = true) {
        pSamplwhk->pPianoRoll->Start();
    }
};

#endif // !_IMIDIENGINE_

当我编译时,它说在pSamplwhk->pPianoRoll->Start();周围:
use of undefined type 'IPianoRoll'
left of '->Start' must point to class/struct/union/generic type

VS2015找到每个编写代码的元素(我没问题),只有在编译(构建)时才会发生。

为什么?我传递了BubblePlug,并转发了IPianoRollIMidiEngine,包括按顺序(在BubblePlug.cpp上)。
IMidiEngine应该了解有关IPianoRoll的所有信息(首先包含在内)。

至少,我应该在“运行时”遇到问题,为什么要在编译?

您可以帮助我了解问题及其解决方法吗?谢谢。

最佳答案

IPianoRoll.h包含IMidiEngine.h,因此无论您以哪个顺序包含两个文件,IPianoRoll的定义始终位于使用它的init函数之后。

避免这种情况的一种方法是将init函数的主体移到单独的.cpp文件中:

在IMidiEngine.h中:

void Init(bool randomScore=true);

在IMidiEngine.cpp中:
void IMidiEngine::Init(bool randomScore) {
    pSamplwhk->pPianoRoll->Start();
}

09-26 18:33