Reputation: 10537
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.
Upvotes: 0
Views: 104
Reputation: 27233
You haven't provided implementation for Destroy()
in B
, e.g.:
template<class T>
void A<T>::B::Destroy(T a) {
// do something
}
Note that you also have other compilation errors: missing semicolon after the body of class B and missing argument in call to Go()
.
Upvotes: 0
Reputation: 477494
Add a definition for virtual void Destroy(T obj);
:
Either in place:
class B
{
public:
virtual void Destroy(T obj) { /* here! */ }
// ==== ALTERNATIVELY: ====
virtual void Destroy(T obj) = 0; // pure-virtual
};
Or afterwards:
template <typename T>
void A<T>::B::Destroy(T obj) { /* here. */ }
In any event, the constructor for A
should take a reference:
A(B & b) : _b(b) { }
//^^^^^
Upvotes: 1
Reputation: 258638
virtual void Destroy(T obj);
isn't implemented. Implement it or mark it as virtual pure = 0
There's no ;
after the definition of X
a.Go();
is wrong, Go
should take a parameter.
Upvotes: 2