C++ g+中奇怪的编译错误+;(叮当作响&编译得很好)

C++ g+中奇怪的编译错误+;(叮当作响&编译得很好),c++,g++,compiler-errors,clang,C++,G++,Compiler Errors,Clang,我试图编译一个实例化这个类的文件。GCC给了我一些隐晦的错误,但clang毫无怨言地编译了它 错误: statemachine.h: In member function ‘void state_machine<Data, T>::start_submachine(void (*)(state_machine<Data, T>&, T), void (*)(state_machine<Data, T>&, T))’: statemachine.

我试图编译一个实例化这个类的文件。GCC给了我一些隐晦的错误,但clang毫无怨言地编译了它

错误:

statemachine.h: In member function ‘void state_machine<Data, T>::start_submachine(void (*)(state_machine<Data, T>&, T), void (*)(state_machine<Data, T>&, T))’:
statemachine.h:245: error: ‘substate_machine<Data, T>::substate_machine(state_machine<Data, T>*)’ is protected
statemachine.h:215: error: within this context
statemachine.h: In member function ‘state_machine<Data, T>* substate_machine<Data, T>::parent()’:
main.cpp:282:   instantiated from here
statemachine.h:138: error: ‘state_machine<Data, T>* state_machine<Data, T>::parent()’ is protected
statemachine.h:241: error: within this context
statemachine.h: In member function ‘void substate_machine<Data, T>::state_return()’:
main.cpp:282:   instantiated from here
statemachine.h:232: error: ‘void state_machine<Data, T>::return_from_sub()’ is protected
statemachine.h:254: error: within this context
statemachine.h:在成员函数“void state\u machine::start\u submachine(void(*)(state\u machine&,T),void(*)(state\u machine&,T))”中:
statemachine.h:245:错误:“substate\u machine::substate\u machine(state\u machine*)”受保护
statemachine.h:215:错误:在此上下文中
statemachine.h:在成员函数“state\u machine*substate\u machine::parent()”中:
main.cpp:282:从此处实例化
statemachine.h:138:错误:“state\u machine*state\u machine::parent()”受保护
statemachine.h:241:错误:在此上下文中
h:在成员函数“void substate_machine::state_return()”中:
main.cpp:282:从此处实例化
statemachine.h:232:错误:“void state_machine::return_from_sub()”受保护
statemachine.h:254:错误:在此上下文中
Main.cpp有282行长,它所指向的行只是一个右大括号}。从未在类外调用Parent()(那么为什么它会抱怨它受到了保护)?为什么它会抱怨state_return()调用受保护的方法,因为它是类的成员。GCC/G++是否会在模板中破坏受保护的数据成员?我怀疑(这只是一种预感)它试图内联扩展宏之类的函数。。。但是为什么呢

守则:

#ifndef STATEMACHINE_H_INC
#define STATEMACHINE_H_INC

//#include <iostream> TODO:  Make better templated stream type
#include <memory>
#include <string>

const std::string null_string = "";

/* Class state_machine:
 *      Templated class to allow easy implementation of FSMs.
 * 
 *      HOW TO USE:
 *          - Make a type containing whatever data needs to be passed
 *            to the current state.
 *          - If necessary, create a preprocessor function run before
 *            the actual state is invoked (prefunc)
 *          - Create a function for each state.  In addition, each
 *            state can be made a submachine by using substate_machine
 *          - If necessary, specialize (INLINE and in the HEADER FILE)
 *            the finalize() method.
 *          - The function names should pretty much be self explanitory.
 *      
 *      Hooray for function pointers.  The code was 5x longer and 10x
 *      buggier before I implemented lexer as a state machine :D.
 * 
 *  NOTE:  This class COPY CONSTRUCTS from the hints provided (at least
 *         for now), so *don't* try and use your old pointer- it's not the
 *         same object!  This was done to simplify this class's
 *         implementation.  At some point I should probably change it...
 * 
 */

template <class Data, class T>
class state_machine {
public:
    //public use typedefs
    typedef void (*state)(state_machine<Data, T>&, T);
    typedef state prefunc_t;
    static void defprefunc(state_machine<Data, T>&, T);
    static void defstate(state_machine<Data, T>&, T);
    static void submachine_handle(state_machine<Data, T>&, T);
    //The above works with submachines because references are treated
    //by the standard like pointers- so polymorphism is allowed
private:
    prefunc_t prefunc;
    //don't feel like writing a full on destructor for one pointer
    std::auto_ptr<Data> internal_data;
    state curstate;
    state returnstate;
    //this MUST be an auto_ptr or our memory management gets REAL tricky
    std::auto_ptr < state_machine<Data, T> > substate;
protected:
    void init();
    void call(state_machine<Data, T>&, T);
    //this method allows submachine to get data from top of hierarchy.
    virtual state_machine<Data, T> * parent(); //return TOP of tree
    void return_from_sub(); //this is a slot, to use the qt term
public:
    //public interface
    state_machine(prefunc_t = defprefunc, Data * = NULL);
    Data& data();
    void change_state(state);
    //TODO:  change std::istream to a stream dependent on T
    //void add_stream(std::istream&);
    void add_char(T);
    //NULL here means curstate:
    void start_submachine(state, state = NULL);
    //this method is available for specialization
    void finalize();
    virtual void state_return();
};

/* class substate_machine:
 *      This class is a helper class to allow the creation of state
 *      machines as states within another state machine.  Submachines:
 *          -Share the same data.
 *          -Behave exactly like a regular state, except upon exiting
 *           the submachine the state should call the state_return()
 *           method, which allows control to flow to the parent machine.
 *          -Are invoked with the start_submachine() method.
 *      Basically, what allows them to share data is the protected
 *      virtual method parent(), which gets the state_machine object
 *      at the hierarchy's root.  This is never used by the submachine,
 *      only in the parent machine methods when accessing shared data
 *      (i.e. the subclass provides 'plug-in' functionality with this
 *      method), so it *could* be made a private virtual, but those seem
 *      to be 1. poorly understood and 2. overprotective in cases like
 *      this (i.e. do we *really* care if the submachine knows how to
 *      access its parent? no, in fact, we encourage it).
 * 
 *      The user should never see this class.  It is only to be used
 *      by the state_machine parent class provide transparent operation
 *      of substates (don't you love polymorphism>)
 */

template <class Data, class T>
class substate_machine : public state_machine<Data, T> {
    state_machine<Data, T> * parentsm; //direct parent state machine
    substate_machine() {} //Default construction causes failure
protected:
    virtual state_machine<Data, T> * parent();
    substate_machine(state_machine<Data, T>*);
public:
    virtual void state_return(); //send a signal to the parent machine
};

// definitions

//note that state_machine<Data, T>::parent() returns the TOP of the
//hierarchy, NOT the direct parent.

template <class Data, class T>
state_machine<Data, T> * state_machine<Data, T>::parent() {
    return this; //base class state machine must be at top of hierarchy
}

template <class Data, class T>
void state_machine<Data, T>::finalize() {
    //this is left to be <intentionally> specialized over
}

template <class Data, class T>
Data& state_machine<Data, T>::data() {
    //use parent here to allow all subs to access the hierarchy's shared
    //data as if they own it.
    return *(parent()->internal_data);
}

//these are two different functions for clarity's sake

template <class Data, class T>
void state_machine<Data, T>::defstate
(state_machine<Data, T>& self, T c) {
    //do nothing - default behavior
}

template <class Data, class T>
void state_machine<Data, T>::defprefunc
(state_machine<Data, T>& self, T c) {
    //do nothing - default behavior
}

template <class Data, class T>
void state_machine<Data, T>::
submachine_handle(state_machine<Data, T>& self, T c) {
    //handle a submachine
    self.substate->curstate(*(self.substate), c);
}

template <class Data, class T>
void state_machine<Data, T>::state_return() {
    //should NOT happen, but handle just in case.
}

template <class Data, class T>
void state_machine<Data, T>::init() {
    curstate = defstate;
    prefunc = defprefunc;
}

template <class Data, class T>
state_machine<Data, T>::state_machine
(prefunc_t func, Data * d) {
    init();
    //make a new data - copy construct if d is not null
    if (d) {
        internal_data = std::auto_ptr<Data>(new Data(*d));
    }
    else {
        internal_data = std::auto_ptr<Data>(new Data);
    }
    prefunc = func;
}

template <class Data, class T>
void state_machine<Data, T>::change_state(state s) {
    curstate = s;
}

//the first state is the state to start a submachine in, the second
//state is the state to go into when the submachine returns to the
//parent, which is by default NULL (the current state)
template <class Data, class T>
void state_machine<Data, T>::start_submachine(state s, state rs) {
    //get arround default argument errors (static resolution...)
    if (rs == NULL) {
        rs = curstate;
    }
    //set up submachines
    substate = std::auto_ptr<state_machine<Data, T> >(new substate_machine<Data, T>(this));
    substate->change_state(s);
    returnstate = rs;
    //set up the submachine state handler
    curstate = submachine_handle;
}

//preprocess and then process a character through the state machine.
template <class Data, class T>
void state_machine<Data, T>::add_char(T c) {
    prefunc(*this, c);
    curstate(*this, c);
}

//this is a slot for the submachine to send its return signal to.
//basically just switches the function pointer back.
template <class Data, class T>
void state_machine<Data, T>::return_from_sub() {
    curstate = returnstate;
}

//now for the substate

template <class Data, class T>
state_machine<Data, T> * substate_machine<Data, T>::parent() {
    //remember, this is the top of the hierarchy.
    return parentsm->parent();
}

template <class Data, class T>
substate_machine<Data, T>::
substate_machine(state_machine<Data, T> * sm) {
    this->init();
    parentsm = sm;
    //initialization.  MUST BE INITIALIZED BY A PARENT THROUGH THIS CTOR
}

template <class Data, class T>
void substate_machine<Data, T>::state_return() {
    parentsm->return_from_sub();
    //sends the parent the return signal.
}

#endif
\ifndef STATEMACHINE\u H\u公司
#定义国家机器公司
//#包括TODO:创建更好的模板化流类型
#包括
#包括
const std::string null_string=“”;
/*类状态机器:
*模板化类,以便轻松实现FSMs。
* 
*如何使用:
*-创建一个包含需要传递的任何数据的类型
*恢复到当前状态。
*-如有必要,在运行前创建预处理器函数
*实际状态被调用(prefunc)
*-为每个状态创建一个函数。此外,每个
*可以使用substate_machine使状态变为子机
*-如有必要,专门化(内联和在头文件中)
*finalize()方法。
*-函数名应该是自解释的。
*      
*函数指针万岁。代码长5倍,长10倍
*在我将lexer作为状态机实现之前,我使用了四轮马车:D。
* 
*注意:这个类从提供的提示中复制构造(至少
*现在),所以*不要*尝试使用你的旧指针-它不是
*同样的目标!这样做是为了简化这个类的
*实施。在某个时候,我可能应该改变它。。。
* 
*/
模板
类状态机{
公众:
//公用typedef
类型定义无效(*状态)(状态机和T);
类型定义状态预功能;
静态无效预失效(状态机和T);
静态无效状态(状态机和T);
静态真空冲床手柄(状态机和T);
//上述方法适用于子机,因为引用是经过处理的
//通过标准的like指针,所以多态性是允许的
私人:
prefunc_t prefunc;
//不想为一个指针编写一个完整的析构函数
std::自动检查ptr内部检查数据;
国家草书;
国家返回国;
//这一定是自动的,否则我们的内存管理会变得非常棘手
std::自动ptr<状态机>子状态;
受保护的:
void init();
无效调用(状态机和T);
//此方法允许submachine从层次结构的顶部获取数据。
虚拟状态_machine*parent();//返回树的顶部
void return_from_sub();//这是一个插槽,使用qt术语
公众:
//公共接口
状态机(prefunc\u t=dexprefunc,Data*=NULL);
数据&数据();
无效变更状态(状态);
//TODO:将std::istream更改为依赖于T的流
//void add_流(std::istream&);
void add_char(T);
//NULL在此处表示游标状态:
void start_子机(状态,状态=NULL);
//此方法可用于专门化
void finalize();
虚拟无效状态_return();
};
/*类子状态机器:
*此类是允许创建状态的帮助器类
*作为另一个状态机中的状态的机器。冲锋机:
*-共享相同的数据。
*-与常规状态完全相同,但退出时除外
*子机器状态应该调用状态_return()
*方法,该方法允许控制流到父计算机。
*-使用start\u submachine()方法调用。
*基本上,允许他们共享数据的是受保护的
*虚拟方法parent(),它获取状态机对象
*在层次结构的根。这是永远不会被冲锋机使用的,
*仅在访问共享数据时在父计算机方法中
*(也就是说,子类提供了与此相关的“插件”功能
*方法),所以它*可以*成为私有虚拟机,但是
*是1。缺乏理解和理解。在类似的情况下过度保护
*这(即,我们*真的*在乎冲锋机是否知道如何
*访问它的父对象?不,事实上,我们鼓励它)。
* 
*用户不应该看到这个类。它只供使用
*由state\u机器父类提供透明操作
*子状态(难道你不喜欢多态性>)
*/
模板
类子状态机:公共状态机{
状态机*parentsm;//直接父状态机
substate_machine(){}//默认构造导致失败
受保护的:
虚拟状态机*parent();
子状态机(状态机*);
公众:
virtual void state_return();//向父计算机发送信号
};
//定义
//请注意,state_machine::parent()返回
//层次结构,而不是直接父级。
模板
状态机*状态机::par
class A
{
protected:
    A(int) {}
public:
    void foo();
};

class B: public A
{
protected:
    B(int):
        A(10) //OK here
     {}

};

void A::foo()
{
    B b(10); //error, that constructor is not accessible to A
}
class A
{
protected:
    void foo() {}
};

class B: public A
{
    void bar() {
        this->foo(); //OK
        B other_b;
        other_b.foo(); //OK
        A a;
        a.foo(); //not OK
        A* b_ptr = &other_b;
        b_ptr->foo(); //not OK, static type of *b_ptr is not B
    }
};