Reputation: 305
I'm using C++ template, and I got the error above.
My code is as follows:
#include <iostream>
#include <string>
using namespace std;
enum type{ONE,TWO,THREE,FOUR,FIVE};
class base{
public:
virtual void baseOut()const =0;
};
template<class T>
class derived:public base{
virtual void drivedOut()const=0;
};
template<class T,type>
class derived2:public derived<T>{
public:
void baseOut(){}
void drivedOut(){}
};
int main(){
derived2<string,ONE> d;
return 0;
}
My question is that since I have overrided pure virtual functions in the class template drived2, why is drived2 still an abstract class? How can I remove this error.
Thank you:)
Upvotes: 3
Views: 8064
Reputation: 1374
Overloading of function alsp depends on the constness of that function.
See http://www.geeksforgeeks.org/function-overloading-and-const-functions/
In you code
virtual void baseOut()const =0;
-- Const function
void baseOut(){}
- non-const function.
Hence it is not function overriding (which you are expecting) but function overloading. Due to which derived2 also becomes an abstract class, as function definition is not present for two virtual functions.
Upvotes: 0
Reputation: 63144
You haven't overriden base::baseOut
, because derived2::baseOut
is non-const
!
void baseOut() const { }
// ^^^^^
The same goes for derived2::derivedOut
.
You should get in the habit of using override
to catch these errors.
void baseOut() override { } // Error, overrides nothing!
void baseOut() const override { } // OK
Clang is more helpful than GCC on this one:
main.cpp:18:14: warning: 'derived2<std::__cxx11::basic_string<char>, type::ONE>::baseOut'
hides overloaded virtual function [-Woverloaded-virtual]
void baseOut(){}
^
[...]
main.cpp:8:22: note: hidden overloaded virtual function 'base::baseOut' declared here:
different qualifiers (const vs none)
virtual void baseOut()const =0;
^
Upvotes: 14