I'm porting http://www.drdobbs.com/embedded-systems/225700666 to Keil MDK
for ARM
microprocessors. The framework compiles and works fine using gcc
on my desktop but using the Keil
compiler gives me an error:
logging/singleton.h(65): error: #70: incomplete type is not allowed
Following code shows the implementation of a singleton
where I get this error. Where does this error come from?
namespace logging {
namespace detail {
template <typename T>
class singleton
{
private:
struct obj
{
obj() { singleton<T>::instance(); }
inline void empty() const { }
};
static obj __obj;
singleton();
public:
typedef T obj_type;
static obj_type & instance()
{
static obj_type obj; // <-- Here I get this error
__obj.empty();
return obj;
}
};
template <typename T>
typename singleton<T>::obj
singleton<T>::__obj;
} /* detail */
} /* logging */
Edit:
The singleton
gets instantiated here
template <typename log_t, typename T>
struct Obj {
static return_type& obj () {
typedef singleton<return_type> log_output;
return log_output::instance();
}
};
where return_type
is a typedef:
typedef R return_type;
and that's a parameter of the parent template:
template<typename Level = ::logging::Void, typename R = loggingReturnType>
class Logger {
...
};
loggingReturnType
is forward declarated above the class definition:
struct loggingReturnType;
Edit 2:
This loggingReturnType
gets generated through following makro.
#define LOGGING_DEFINE_OUTPUT(BASE) \
namespace logging { \
struct loggingReturnType : public BASE { \
/*! \brief The provided typedef is used for compile time \
* selection of different implementation of the \
* %logging framework. Thus, it is necessary \
* that any output type supports this type \
* definition, why it is defined here. \
*/ \
typedef BASE output_base_type; \
}; \
}
This makro gets called in a config header.
Edit 3:
Her is a link to the preprocessor output: http://www.pasteall.org/31617/cpp. This file compiles pretty using g++
. The definition of loggingReturnType
is the last before the main
- so the singleton doesn't the exact type but nevertheless it works. I also looked at the preprocessor output of the Keil
compiler and it's almost the same.
So what went wrong here?
According to the information you put here, the error message makes perfect sense. The code is trying to created an instance of an object on the stack. This object's type is only forward declared but no definition is available to the compiler at that time.
You need to make the definition of this type available to the compiler before it has to create an instance of this type.