Reputation: 33
The problem I am facing is somehow related to "loop inclusion" or "incompleted class"
I have 2 classes:
ControllerManager, this class is declared as Singleton, this class has a object of AuxController.
AuxController, this class has a function that needs to get the instance of ControllerManager
The problem is: when compiling source code, it fails with error "incomplete type" or "invalid type"
Is there any way to fix this problem? or is there any other way to redesign code structure?
Source code 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;
}
Upvotes: 0
Views: 613
Reputation: 9735
The problem consists into a cycle dependency.
In order to solve that problem: use forward declaration and include all needed header only in the cpp (compilation-unit) file.
In ControllerManager.hpp:
class AuxController;
class ControllerManager {
// ...
public: AuxController* m_auxcontroller;
};
// Remember to not use m_auxcontroller in the header file, JUST declaration are allowed.
Note: you have to use pointers or references to the forwarded class, so remember to initialize them correctly.
In the ControllerManager.cpp:
#include "ControllerManager.hpp"
#include "AuxController.hpp" // In Cpp file include everything you need.
// ...
The same in the class AuxController
.
In the AuxController.hpp:
// You dont need header of other file in this case.
class AuxController {
// ...
};
In AuxController.cpp:
#include "AuxController.hpp"
#include "ControllerManager.hpp"
// ...
Upvotes: 2
Reputation: 4901
Do not include "controllermanager.h" inside auxcontroller.h, but do include it inside auxcontroller.cpp.
In general you should not include header files by other header files if it can be avoided. Use forward declaration instead. But do include all required header files from cpp files.
Upvotes: 3