2016-08-31 155 views
-1

我面對的是主題相關的「循環包容」或「未完類」無法從其他類

的問題得到一個單獨的類的實例我有2類:

ControllerManager的,這個類聲明爲Singleton,這個類有一個對象AuxController

AuxController,這個類需要得到實例ControllerManager的

的問題是一個函數:編譯源代碼時,它失敗,出現錯誤「不完全型」或「無效類型」

有什麼辦法解決這個問題嗎? 還是有任何其他方式來重新設計代碼結構?

源代碼 ControllerManager.h

#ifndef CONTROLLERMANAGER_H 
#define CONTROLLERMANAGER_H 
#include "auxcontroller.h" 

class ControllerManager 
{ 
/* This class is defined as Singleton class */ 
private: 
    /* 1. define a private static instance */ 
    static ControllerManager *inst; 

public: 
    /* 2. define a public static accessor */ 
    static ControllerManager *getInstance(){ 
     /* 3. do lazy initialization */ 
     if(!inst){ 
      inst = new ControllerManager(); 
     } 
     return inst; 
    } 

protected: 
    /* 4. Define all accessors to be protected */ 
    ControllerManager(); 
    ~ControllerManager(); 

/* property */ 
private: 
    int m_code; 

public: 
    int getCode() 
    { 
     return m_code; 
    } 

    void setCode(int _code) 
    { 
     m_code = _code; 
    } 

/* below code causes fail of compilation */ 
public: 

    AuxController m_auxcontroller; 

}; 

#endif // CONTROLLERMANAGER_H 

ControllerManager.cpp

#include "controllermanager.h" 

/* 5. initialize static variable */ 
ControllerManager *ControllerManager::inst = 0; 
ControllerManager::ControllerManager() 
{ 
    m_code = 15; 
} 

ControllerManager::~ControllerManager() 
{ 
    delete inst; 
} 

AuxController.h

#ifndef AUXCONTROLLER_H 
#define AUXCONTROLLER_H 

/* if do NOT include controllermanager.h with below line, 
* and declare ControllerManager class as a forward declaration: 
* 
* class ControllerManager; 
* 
* compiler will stop due to "incomplete type" 
*/ 
#include "controllermanager.h" 



class AuxController 
{ 
public: 
    AuxController(); 
    void setControllerCode(int code); 
}; 

#endif // AUXCONTROLLER_H 

AuxController.cpp

#include "auxcontroller.h" 

AuxController::AuxController() 
{ 

} 

void AuxController::setControllerCode(int code) 
{ 
    /* if do NOT include controllermanager.h , 
    * and declare ControllerManager class as a forward declaration in the header file: 
    * 
    * class ControllerManager; 
    * 
    * compiler will stop due to "incomplete type" at this line 
    * 
    */ 
    ControllerManager::getInstance()->setCode(code); 
} 

的main.cpp

#include "controllermanager.h" 

int main(int argc, char *argv[]) 
{ 
    ControllerManager *ctlMng = ControllerManager::getInstance(); 
    ctlMng->setCode(10); 
    return 0; 
} 
+0

請把錯誤信息逐字在你的問題。我懷疑你有一個循環包含問題或類似的錯誤信息是這樣的典型症狀。 –

回答

2

不包括內部auxcontroller.h 「controllermanager.h」,但確實包括這裏面auxcontroller.cpp。

一般而言,如果可以避免的話,您不應該在其他頭文件中包含頭文件。改用前向聲明。但是請包含所有必需的cpp文件頭文件。

+0

這也是:即使您必須轉發聲明,您仍然必須將頭文件包含在cpp文件中。 – Hayt

+0

謝謝,它現在有效 – TrongVu

+0

歡迎您,如果它解決了您的問題,請接受答案。 – michalsrb

1

問題在於循環依賴

爲了解決這個問題:使用forward declaration並且只包含cpp(編譯單元)文件中的所有需要​​的標題。

ControllerManager.hpp

class AuxController; 

class ControllerManager { 
    // ... 
    public: AuxController* m_auxcontroller; 
}; 

// Remember to not use m_auxcontroller in the header file, JUST declaration are allowed. 

注:你必須使用指針引用到轉發類,所以記得要正確初始化。

ControllerManager中。CPP

#include "ControllerManager.hpp" 
#include "AuxController.hpp" // In Cpp file include everything you need. 
// ... 

在類AuxController一樣。

AuxController.hpp

// You dont need header of other file in this case. 

class AuxController { 
    // ... 
}; 

AuxController.cpp

#include "AuxController.hpp" 
#include "ControllerManager.hpp" 

// ... 
+0

從我的理解,這不會編譯,因爲你沒有使用指向正向聲明的類'AuxController'的指針。 'm_auxcontroller'必須是指針或引用。 –

+0

@TheBadger你說得對,太快了!我要修復 –

+0

謝謝,它現在起作用 – TrongVu