首先,我设法在一个头文件中实现一个状态机。我知道我需要一些前向声明,我必须从外部到内部定义状态。我真正不明白的是:如何处理多个文件?
我的妻子:
然后看起来是这样的:
// forward.h
struct Machine;
struct StA;
struct StB;
// machine.h
#include "forward.h"
struct Machine : sc::state_machine< Machine, StA > {};
// a.h
#include "forward.h" // for StB
#include "machine.h"
struct StA : sc::simple_state< StA, Machine, StB > {};
// b.h
#include "forward.h"
#include "a.h"
struct StB : sc::simple_state< StB, StA > {};现在剩下的是如何将所有的事情都包括在程序中。我的想法是有一个标题,其中包括从外部到内部所有州的头。
// the_machine.h
#include "forward.h"
#include "machine.h"
#include "a.h"
#include "b.h"
// use this header now where you need the state machine但是,我不知道总的想法是否合适,即使这样,我也无法编译(嗯,不是这个,而是我按照这个设计原则构建的一台机器)。在一个文件中完成所有的操作非常容易,一旦您了解了上下文需要完成,状态需要转发,声明等等,但是由于复杂性和维护原因,分裂会让我神经紧张……Incomplete type 'StXY' used in nested name specifier等等。
发布于 2013-01-18 09:20:08
如果您混淆了包含头的顺序,则通常会出现Incomplete type错误。
尝试折叠:创建一个只包含the_machine.h的空the_machine.h,并只对其进行预编译。对于编写包含预处理翻译单元的文件的不同编译器,有命令行标志(即,一个文件con0taining,编译器看到的所有代码)。检查该文件,看看是否一切都按照您认为的顺序进行。大多数预处理器生成#line控制命令,告诉编译器(和您)所查看的头/源的行。
编辑:
如果希望只使用#include machine.h,则必须在机器定义之后包含状态定义。乍一看,这可能看起来很奇怪,但如果您想要拆分依赖部分,那么它通常是如何与模板一起工作的。许多人对后面包含的部分使用不同的文件后缀,因为它们本身并不是真正的标题,也就是说,不能只包含它们。示例:
//Something.h
template <class T>
struct Something
{
void meow(T const& t);
int wuff(T const& t, int b);
};
#include "Something.impl" //or .ipp, or other endings...
//Something.impl
template <class T>
void Something<T>::meow(T const& t)
{ /* implement... */ }
template <class T>
int Something<T>::wuff(T const& t, int b)
{ /* implement... */ }您的机器.h看起来类似-定义机器,并包含其后状态的实现。我不会将状态的实现文件命名为X.h,因为它们不是可以单独包含和使用的单个标头。
https://stackoverflow.com/questions/14395044
复制相似问题