I have an interface and am trying to make my templated class implement this interface. A simple example to demonstrate the problem is:
class Base{
virtual void do_something(int a) = 0;
virtual ~Base();
};
template<typename T>
class Concrete : Base{
T _t;
Concrete(T t):_t(t){};
virtual void do_something(int a);
};
template<typename T>
virtual void Concrete<T>::do_something(int a){
std::cout << a << std::endl;
}
int main(int argc, char **argv) {
Concrete<int> c;
c.do_something(5);
}
However the compiler complains that:
error: templates may not be 'virtual'
virtual void Concrete<T>::do_something(int a){
Is there a way to achieve this behaviour?
Yes, what n.m. said. Clang's error message is a little more helpful here:
so.cpp:19:1: error: 'virtual' can only be specified inside the class definition
virtual void Concrete<T>::do_something(int a){
^~~~~~~~
Remove the virtual keyword (writing it inside the class definition is enough/the only correct way).
Your code has a few other problems (~Base() is not defined, Concrete() expects a parameter), but the problem you ask about is solved by simply deleting the erroneous virtual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With