CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2004
    Posts
    41

    Cool compiling a templated class

    what is the correct way to compile a class that uses templates. i keep getting an error msg on the linking step. can some one help?

    "Stack.h"
    Code:
     
    #pragmaonce
     
    template <class xtype>
    struct node
    {
    	 xtype info;
    	 node * next;
    };
     
    template<class xtype>
    class Stack
    {
    private:
    	 node<xtype> * top;
    public:
    	 Stack(void);
    	 ~Stack(void);
    	 void Push(xtype);
    	 void Pop(xtype&);
    	 bool Isfull(void);
    	 bool Isempty(void);
    };
     
    
    "Stack.cpp"
    Code:
     
    #include"Stack.h"
     
    template <class xtype> 
    Stack<xtype>::Stack(void)
    {
    	 top = NULL;
    }
     
    template <class xtype> 
    Stack<xtype>::~Stack(void)
    {
    	 node<xtype>* q;
    	 while(top != NULL)
    	 {
    		  q = top;
    		  top = top->next;
    		  delete q;
    	 }
    }
     
    template <class xtype>
    void Stack<xtype>::Push(xtype item)
    {
    	 node<xtype>* p;
    	 p = new node<xtype>;
    	 p->next = top;
    	 p->info = item;
    	 top = p;
    }
     
    template <class xtype>
    void Stack<xtype>::Pop(xtype& item)
    {
    	 if( !Isempty() ) 
    	 {
    		  item = top->info;
    		  node<xtype>* p;
    		  p = top;
    		  top = top->next;
    		  delete p;
    	 }
     
    }
     
    template <class xtype>
    bool Stack<xtype>::Isfull()
    {
    	 node<xtype>* freestore;
    	 freestore = new node<xtype>;
     
    	 if(freestore == NULL)
    		  return true;
    	 else
    	 {
    		  delete freestore;
    		  return false;
    	 }
     
    }
     
    template <class xtype>
    bool Stack<xtype>::Isempty()
    {
    	 return (top == NULL);
    }
     
    
    "main.cpp"
    Code:
    #include"Stack.h"
    using namespace std;
    int main()
    {
    	 Stack<int> q;
    return 0;
     
    }
     
    
    EROR
    Code:
     
    ------ Build started: Project: LinkedStack, Configuration: Debug Win32 ------
     
    Compiling...
     
    main.cpp
     
    Stack.cpp
     
    Generating Code...
     
    Linking...
     
    main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ) referenced in function _main
     
    main.obj : error LNK2019: unresolved external symbol "public: __thiscall Stack<int>::Stack<int>(void)" (??0?$Stack@H@@QAE@XZ) referenced in function _main
     
    C:\CPLUSPLUS\LinkedStack\Debug\LinkedStack.exe : fatal error LNK1120: 2 unresolved externals
     
    Build log was saved at "file://c:\CPLUSPLUS\LinkedStack\Debug\BuildLog.htm"
     
    LinkedStack - 3 error(s), 0 warning(s)
     
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     
    

  2. #2
    Join Date
    Dec 2005
    Posts
    642

    Re: compiling a templated class

    remove "#include "stack.h"" from stack.cpp, and instead put "#include "stack.cpp"" at the bottom of stack.h.

  3. #3
    Join Date
    Jul 2004
    Posts
    41

    Re: compiling a templated class

    thanks googler for the help but I've tried that did it work for you. I get the infamous error:
    .\Stack.cpp(3) : error C2143: syntax error : missing ';' before '<'
    which I think means that my template is illegal somehow. these are all the errors i get
    "ERRORS"
    Code:
      
    ------ Build started: Project: LinkedStack, Configuration: Debug Win32 ------
    
    Compiling...
    
    Stack.cpp
    
    .\Stack.cpp(3) : error C2143: syntax error : missing ';' before '<'
    
    .\Stack.cpp(3) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    
    .\Stack.cpp(3) : error C2988: unrecognizable template declaration/definition
    
    .\Stack.cpp(3) : error C2059: syntax error : '<'
    
    .\Stack.cpp(8) : error C2588: '::~Stack' : illegal global destructor
    
    .\Stack.cpp(8) : fatal error C1903: unable to recover from previous error(s); stopping compilation
    
    main.cpp
    
    Generating Code...
    
    Build log was saved at "file://c:\CPLUSPLUS\LinkedStack\Debug\BuildLog.htm"
    
    LinkedStack - 6 error(s), 0 warning(s)
    
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    
    


  4. #4
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: compiling a templated class

    For most compilers you have to keep template-code ( declaration and implementation ) in one file. Looks like you are using an MS-compiler. This one cannot handle the extern - keyword.
    The problem is that the compiler cannot know where you have put the implementation of your template-functions if you #include the declaration-part only.
    BTW you cannot compile templates. Code is generated when you instantiate a template.
    K

  5. #5
    Join Date
    Dec 2005
    Posts
    642

    Lightbulb Re: compiling a templated class

    Quote Originally Posted by sublyme
    thanks googler for the help but I've tried that did it work for you.
    Oops, sorry, I forgot. You shouldn't compile Stack.cpp directly as a source file in your project. Use the project settings to mark it as "exclude from build". You should only compile main.cpp in your example.

  6. #6
    Join Date
    Jul 2004
    Posts
    41

    Re: compiling a templated class

    thanks for the help guys i really appreciate it. everything works fine now i did what you said googler.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: compiling a templated class

    Have a look at this FAQ.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured