Странные ошибки компиляции в g ++ (clang ++ компилируется нормально) - PullRequest
2 голосов
/ 17 февраля 2011

Я пытаюсь скомпилировать файл, который создает экземпляр этого класса.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

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

Заранее спасибо за любой ввод.Я бы пометил clang ++, но это не позволило мне ...

Ответы [ 3 ]

1 голос
/ 17 февраля 2011

Эти вещи защищены, как говорит компилятор. Родительский класс не может вызвать защищенный конструктор производного класса (он работает по-другому).

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
}

И родительский метод substate_machine действительно пытается вызвать защищенный метод через указатель, статический тип которого не является substate_machine.

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
    }
};

Интересно, вы ожидаете, что protected будет означать "любой класс может получить доступ к защищенным частям любого другого класса, если оба класса принадлежат одному и тому же дереву наследования"? ...

0 голосов
/ 30 июля 2013

Это похоже на ошибку в g ++. Он не должен проверять доступ к конструктору substate_machine<Data, T>, потому что он имеет зависимый тип. Функция, содержащая new-expression , может быть создана только для типов, где substate_machine является специализированным и имеет конструктор public, поэтому компилятору не разрешено отклонять этот код.

Я не могу найти версию g ++, в которой есть эта ошибка; какую версию вы используете?

0 голосов
/ 17 февраля 2011

Я считаю, что g ++ - это правильно.Похоже, вы вызываете защищенный инициализирующий конструктор одного шаблона из другого шаблона.Он в другом классе и поэтому недоступен.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...