I have a quick question. I am writing C++ code; I have two classes in the same file. One inherits from the other, and I am trying to use templates to make the classes more general.
Here is the file for the base class:
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
};
And here is the inherited class:
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();
}
}
};
However, when I try this, I get the following errors:
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
It seems that the Exec (ie: the base) class isn't getting properly declared; if I include the typedef and the instance variables from the base class in the inherited class, I don't get these errors. However, if I include everything from the base class, then its pointless to use inheritance!
I've tried doing a "declaration" of the base class, as some have recommended (ie: class Base;), but that doesn't seem to help.
I've been doing some google-fu on this for some hours; if anyone has any ideas, that'd be super!
You need to say typename Exec<E>::Exe
. Because the baseclass is dependent. Same for Execute, you need to qualify the call with the baseclass name in front: Exec<E>::Execute();
.
Otherwise those unqualified names ignore the dependent base class.
Exec<E>::
(for types and functions) or this->
(for member variables) - Mooing Duck 2012-04-04 18:29
BaseClass::
or this->
as appropriate - Johannes Schaub - litb 2012-04-04 20:22
typename CondExec::Exe
,this->ThisObj
andthis->Execute()
- Luc Danton 2012-04-04 18:15