c++ - Trouble with templates -


i have 3 files , want compile , run them, keep getting errors , warnings. redefinition of struct node< t >. don't know templates, looks right me. and, spent lot of time trying figure out whats wrong. thanks.

//mystack.h #ifndef mystack_h #define mystack_h  template <class t> struct node {     t info;     t *next; };  template <class t> class mystack { private:      struct node<t> *top; public:      void push(t item);      void pop();      int top();      void print();   };  #endif 

//mystack.cpp #include <iostream> #include "mystack.h"  template <class t> struct node {        t info;     t* next;  };  template <class t> class mystack { private:      struct node<t>* top;  public:       void push(t item)      {         if(top == null)         {             top = new( struct node<t> );             top->info = item;             top->next = null;         } else          {             node<t>* temp;             temp = top;             top = new( struct node<t> );             top->info = item;             top->next = temp;         }     }      void pop()     {         if( top == null )         {         } else          {                 node<t>* temp;             temp = top->next;             delete top;             top = temp;         }     }      int top()     {         return top;      }      void print()      {            if(top != null)         {             node<t>* temp;             temp = top;             while(temp != null)             {                 std::cout << temp << std::endl;                 temp = temp->next;             }         }     }    }; 

if designing class instead of template, you're doing wrong because you're redefining types.

but since you're writing templates, you're wrong earlier that: can't separately compile templates.

brief pointer on c++ compilation model:

// definition of node template<typename t> struct node {     t info;     t* next; // shouldn't node*? };  // definition of mystack template <typename t> class mystack { private:      node<t> *top; public:      // declarations, not definitions, of mystack function members.     void push(t item);     void pop();     int top();     void print();   };  // example definition of mystack::push template<typename t> void mystack<t>::push(t item) {     // before } 

the type definitions appear in headers (if reused in different tus) include guards doing. guards here because definitions must appear @ once per tu. do not manually repeat type definition (e.g. in source file did). wrong should be: nobody wants copy-n-paste errors.

the function members definitions appear in source files, unless members of template. in latter case it's simpler put them in headers (they don't have inline either).

you can learn details of compilation model elsewhere on so, or in books, or on internet. searching 'template definition' or 'one definition rule' (or odr) can help.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -