So I have the following code:
template<class T>
class A
{
public:
class B
{
public:
virtual void Destroy(T obj);
};
B &_b;
A(B b) : _b(b)
{
}
void Go(T obj)
{
_b.Destroy(obj);
}
};
class X : public A<int>::B
{
public:
void Destroy(int x)
{
//do something
}
};
int main()
{
X x;
A<int> a(x);
a.Go(5);
return 0;
}
But I get a compile error:
undefined reference to 'A<int>::B::Destroy(int)'
I’ve seen issues before when doing templates in separate .hpp and .cpp files… but this is all in one file.
Thanks.
Add a definition for
virtual void Destroy(T obj);:Either in place:
Or afterwards:
In any event, the constructor for
Ashould take a reference: