我正在将 http://www.drdobbs.com/embedded-systems/225700666 移植到
Keil MDK
微处理器。该框架在我的桌面上使用 ARM
编译并运行良好,但使用 gcc
编译器给我一个错误:Keil
以下代码显示了
logging/singleton.h(65): error: #70: incomplete type is not allowed
的实现,我在其中出现此错误。这个错误从何而来?
singleton
编辑:
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 */
在这里被实例化
singleton
其中
template <typename log_t, typename T>
struct Obj {
static return_type& obj () {
typedef singleton<return_type> log_output;
return log_output::instance();
}
};
是 typedef:
return_type
这是父模板的参数:
typedef R return_type;
template<typename Level = ::logging::Void, typename R = loggingReturnType>
class Logger {
...
};
在类定义之上向前声明:
loggingReturnType
编辑2: 这个
struct loggingReturnType;
是通过以下makro生成的。
loggingReturnType
此 makro 在配置标头中被调用。
编辑3: 她是预处理器输出的链接:
http://www.pasteall.org/31617/cppg++
的定义是loggingReturnType
之前的最后一个 - 因此单例不是确切的类型,但它仍然有效。我还查看了 main
编译器的预处理器输出,几乎是一样的。那么这里出了什么问题?
您需要先让编译器可以使用此类型的定义,然后才能创建此类型的实例。
添加到已接受的答案中,作为解决方案,供其他人来这里获取相同的错误代码。单例实例应该是
Keil
类型:
因此你需要将私有单例实例设置为指针类型。pointer*
正如 Selalerer 的回答所述:
代码尝试在堆栈上创建对象的实例。该对象的类型仅被前向声明,但当时编译器没有可用的定义。
您需要先让编译器可以使用此类型的定义,然后才能创建此类型的实例。