c++ - How to declare destructor of a templated class -


i error while trying compile following class

stack.cpp:28: error: expected constructor, destructor, or type conversion before ‘::’ token

#include <iostream> using namespace std;  template <class t> class stack { public:     stack(): head(null) {};     ~stack();      void push(t *);     t* pop();  protected:     class element {     public:             element(element * next_, t * data_):next(next_), data(data_) {}             element * getnext() const { return next; }             t * value() const {return data;}     private:             element * next;             t * data;     };      element * head; };  stack::~stack() {     while(head)     {             element * next = head->getnext();             delete head;             head = next;       }  } 

you declaring template class. can either:

  • implement destructor within class declaration, this

    public:     stack(): head(null) {};     ~stack() {         // ...     } 
  • define templated destructor outside class declaration, this

    template <class t> stack<t>::~stack() {     // ... } 

however, if attempt define stack::~stack() compiler not know type t implementing destructor for.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -