我面临的问题与“循环包含”或“未完成的类”
有某种关联我有2个班级:
ControllerManager ,此类声明为Singleton,此类具有 AuxController 的对象。
AuxController ,这个类有一个需要获取 ControllerManager
实例的函数问题是:在编译源代码时,它失败并显示错误“不完整类型”或“无效类型”
有没有办法解决这个问题? 或者有没有其他方法来重新设计代码结构?
源代码 ControllerManager.h
$(document).ready(function(){
$("#select").click(function(){
var values= [];
$(".car > option").each(function(){
values.push($(this).attr('value'));
});
$('.car').val(values);
});
$("#unselect").click(function(){
$('.car').val([]);
});
});
ControllerManager.cpp
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select class="car" multiple size="3">
<option value="volvo" selected>Volvo</option>
<option value="saab" selected>Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<button id="select">Select All</button>
<button id="unselect">Unselect All</button>
</button>
AuxController.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
AuxController.cpp
#include "controllermanager.h"
/* 5. initialize static variable */
ControllerManager *ControllerManager::inst = 0;
ControllerManager::ControllerManager()
{
m_code = 15;
}
ControllerManager::~ControllerManager()
{
delete inst;
}
的main.cpp
#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
答案 0 :(得分:3)
不要在auxcontroller.h中包含“controllermanager.h”,但请将它包含在auxcontroller.cpp中。
通常,如果可以避免,则不应包含其他头文件的头文件。请改用前向声明。但是请包含cpp文件中所有必需的头文件。
答案 1 :(得分:2)
问题在于循环依赖。
为了解决该问题:使用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"
// ...