I want to define a class like,
template<int N> class A{
public:
template<int D> B<D>* InstanceofB;
void methodofClassA(input){
methodofClassB(input);
}
}
where,
template<int X> class B{
Some methods..
}
The goal is to associate a specific instance of class B with an instance of class A, such that on invoking methodofClassA(input) it uses this specific instance of class B to evaluate and return an expression.
However, I get an error in class A declaration as “member InstanceofB declared as a template.
How do I resolve this?
Indrajit Wadgaonkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
You could add the template parameter to A
template<int X> class B {};
template<int N, int D> class A{
public:
B<D>* InstanceofB;
void methodofClassA(){
}
};
int main() {
//Instantiate A with both template params
A<1,2> a;
}
Juste note that to call the methodofClassB you’d have to write
InstanceofB->methodofClassB(input);
1
When you have a variable template as data member, it needs to be static
.
Thus, the error can be solve as shown below:
template<int X> class B{
};
template<int N> class A{
public:
//------------------vvvvvv--------------------->added static here
template<int D> static B<D>* InstanceofB;
void methodofClassA()
{
}
};
Demo
6