编译时间" null"使用前瞻性声明?

时间:2016-08-01 08:48:30

标签: c++ pointers compilation compiler-errors forward-declaration

我在使用远期声明时遇到问题,而且我不知道如何修复它。这是我的文件:

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找到编写代码的每个元素(我没有问题),它只在我编译(Build)时才会发生。

为什么呢?我通过了BubblePlug,我向前推进了IPianoRollIMidiEngine,包括它们(在BubblePlug.cpp上)。

IMidiEngine应该了解IPianoRoll的所有内容(首先包含它)。

至少,我应该在"运行时"有问题,为什么在编译?

您能帮助我了解问题以及如何解决问题吗?感谢。

1 个答案:

答案 0 :(得分:1)

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