1

私はフォワード宣言を使用している問題に直面しており、それを修正する方法はわかりません。ここに私のファイルです:フォワード宣言を使用してコンパイル時に "null"が発生しましたか?

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_  

I をコンパイルし、それが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(これが最初に含まれています)を知っている必要があります。

少なくとも、「実行時」に問題があるはずです。なぜコンパイルするのですか?

問題を理解し、解決する方法を教えていただけますか?ありがとう。

+0

IPianoRoll.hに '#include" IMidiEngine.h "を追加してみてください – alexeykuzmin0

+0

@ alexeykuzmin0:私は質問を編集しました。見てみな! – markzzz

+1

無関係:あなたのインクルードガードは違法です。 https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier –

答えて

1

IPianoRoll.hにはIMidiEngine.hが含まれているため、2つのファイルをどの順序で含めるにしても、IPianoRollの定義は常に使用されるinit関数の後に来ます。 IMidiEngine.hで

:IMidiEngine.cppで

void Init(bool randomScore=true); 

void IMidiEngine::Init(bool randomScore) { 
    pSamplwhk->pPianoRoll->Start(); 
} 
これを回避する

一つの方法は、別の.cppファイルにinit関数の本体を移動させることです

+0

今、私は何が問題かを見ています。 "once"を追加するので、IPianoRollは常にIMidiEngineの後になります。 IMidiEngineはIPianoRollのStartメソッドを知らないのですか?なぜファイルをspearateすると問題が解決するのでしょうか?それは私が誇りに思っていないものです。コンパイル時に最初に.hを含めると、 "compile" .cppよりも? – markzzz

+1

クラスの定義は、使用を許可される前に確認する必要があります。これは私の提案する解決策です。つまり、関数本体をコンパイルの後の時点に移動します。コンパイラは、両方の定義をすでにコンパイラに見せています。 –

+1

@paizza - .cppファイルに*すべてが*最初に含まれています - 関数の本文に表示されます。ヘッダーファイルでは、次のヘッダーに何が入っているのかわかりません。 –

関連する問題