c++中的继承、作用域和模板构造函数
inheritance, scope, and template constructors in c++
我有一个快速问题。我正在编写C++代码;我在同一个文件中有两个类。一个从另一个继承,我正在尝试使用模板使类更通用。
这是基类的文件:
template<class E> // this is the class we will execute upon
class Exec{
protected:
typedef void (*Exe)(E*); // define a function pointer which acts on our template class.
Exe* ThisFunc; // the instance of a pointer function to act on the object
E* ThisObj; // the object upon which our pointer function will act
public:
Exec(Exe* func, E* toAct){ThisFunc = func; ThisObj=toAct;}
Exec(){;} // empty constructor
void Execute(){ThisFunc(ThisObj);} // here, we pass our object to the function
};
这是继承的类:
template<class E> // this is the class we will execute upon
class CondExec : protected Exec<E>{ // need the template!
protected:
typedef bool (*Cond)(E*); // a function returning a bool, taking a template class
Cond* ThisCondition;
public:
CondExec(Exe* func, E* toAct,Cond* condition): Exec<E>(func,toAct){ThisCondition=condition;}
void ExecuteConditionally(){
if (ThisCondition(ThisObj)){
Execute();
}
}
};
然而,当我尝试这样做时,我会得到以下错误:
executables.cpp:35: error: expected `)' before ‘*’ token
executables.cpp: In member function ‘void CondExec<E>::ExecuteConditionally()’:
executables.cpp:37: error: ‘ThisObj’ was not declared in this scope
executables.cpp:37: error: there are no arguments to ‘Execute’ that depend on a template parameter, so a declaration of ‘Execute’ must be available
Exec(即:基类)类似乎没有得到正确的声明;如果我在继承的类中包含基类的typedef和实例变量,我就不会得到这些错误。然而,如果我包含基类中的所有内容,那么使用继承就没有意义了!
正如一些人所建议的那样,我尝试过对基类进行"声明"(例如:class base;),但这似乎没有帮助。
我已经在谷歌上做了几个小时了;如果有人有任何想法,那就太棒了!
您需要说typename Exec<E>::Exe
。因为基类是依赖的。与Execute相同,您需要使用前面的基类名称:Exec<E>::Execute();
来限定调用。
否则,那些不合格的名称将忽略依赖基类。
相关文章:
- "变量":函数中函数作用域不允许初始化的自动或寄存器变量'naked'
- 当变量在多个函数作用域中使用时,我应该在类 private 中声明该变量吗?
- 缩短成员函数作用域说明符(嵌套类)(C++)
- 函数作用域是静态变量还是线程本地变量在C++11中的第一个条目中初始化
- 如何声明在函数作用域中定义的结构
- 我可以在函数作用域中使用定义指令吗?
- 为什么函数作用域不允许类前向声明?
- C++函数作用域中声明的返回引用
- 关于 C++(或 C)中的指针的函数作用域
- 如何在与 c++ 中定义的函数作用域相同的文件中限制函数作用域
- 函数作用域的静态非 Pod 对象初始化
- C++函数作用域
- 正在获取另一个函数作用域中的参数值
- 对临时对象的常量引用在函数作用域(生存期)后被破坏
- 被视为依赖作用域的函数模板的函数作用域结构体的静态成员函数
- 局部作用域和函数作用域的区别
- 函数作用域中的指针崩溃
- 将指针传递给函数只会改变函数作用域中的值
- 友元函数作用域和声明点
- using声明不能在函数作用域中重复.为什么