I am trying to implement a template which would allow to create a class derived from vector<> such that on deletion the element of the vector are deleted. The below snippet represents an attempt to do so:
include <vector>
using namespace std;
template <class P>
class TidyVector : public vector<P> {
public:
~TidyVector() {
while (vector<P>::size()) {
P pi = vector<P>::back();
vector<P>::pop_back();
delete pi;
}
}
}
TidyVector<int*> i;
Attempts to compile this using g++ -c try.cc result in the following error messages:
try.cc:1: error: expected constructor, destructor, or type conversion before '<' token
try.cc:6: error: expected template-name before '<' token
try.cc:6: error: expected `{' before '<' token
try.cc:6: error: expected unqualified-id before '<' token
try.cc:17: error: aggregate 'TidyVector<int*> i' has incomplete type and cannot be defined
What is going on - why does this not work? Or, maybe, a more appropriate question to ask: what is the standard way to deal with this situation (automated clean up of the vector on deletion)?