gpt4 book ai didi

c++ - g++ 中的奇怪编译错误(clang++ 编译正常)

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:35:34 26 4
gpt4 key购买 nike

我正在尝试编译一个实例化此类的文件。 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() 调用 protected 方法,因为这类的成员。 GCC/G++ 是否搞砸了模板中 protected 数据成员?我怀疑(这只是一种预感)它正在尝试内联扩展像宏这样的函数……但为什么呢?

代码:

#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++ 标记,但它不允许我...

最佳答案

正如编译器所说,这些东西都受到了保护。父类不能调用派生类的 protected 构造函数(反之亦然)。

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 的指针调用 protected 方法。

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 表示“只要两个类属于同一继承树,任何类都可以访问任何其他类的 protected 部分”?...

关于c++ - g++ 中的奇怪编译错误(clang++ 编译正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5023145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com