I have a strange error while compiling template classes. This is my minimal example:
#include <iostream>
using namespace std;
template<class T>
class A{
T element;
protected:
virtual bool empty() const noexcept = 0;
};
template<class T>
class B : public A<T> {
public:
bool tryOP(T &element) const {
if (A<T>::empty())
return false;
else
{
std::cout << "Operation" << std::endl;
return true;
}
}
};
template <class T>
class C : public B<T> {
private:
bool empty() const noexcept override{
return true;
}
};
int main(){
int n = 0;
C<int> c;
c.tryOP(n);
}
The error:
/tmp/ccJvMBCZ.o: In function B::tryOP(int&) const: A.cpp:(.text._ZNK1BIiE5tryOPERi[_ZNK1BIiE5tryOPERi]+0x18): undefined reference to `A::empty() const' collect2: error: ld returned 1 exit status
Why undefined reference to empty()? It's right there!