another LNK2001 error problem

This is a discussion on another LNK2001 error problem within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I'm using VisualC++ version 6.0 to make a console application. Get the following error: sandbox.obj : error LNK2001: unresolved ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    another LNK2001 error problem

    Hello, I'm using VisualC++ version 6.0 to make a console application.

    Get the following error:
    sandbox.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ)

    Actuall, get 6 similar errors, one for each member function.

    If I cut and paste stack.cpp code directly into sandbox.cpp everything works fine. And stack.h and stack.cpp are in the project.

    Here's the code:

    sandbox.cpp

    Code:
    #include "../Libraries/include/stack.h"
    
    int main() {
    
    Stack<int> s;
    
    return 0;
    
    }
    =======================
    stack.h

    Code:
    template<class T> class Stack {
    private:
       struct StackNode {
          T data;
          StackNode * next;
    
          StackNode( const T& newData,
          StackNode *nextNode ):
          data( newData ), next( nextNode ) {}
       };
       StackNode * top;
       int size;
    
    public:
       Stack();
       ~Stack();
    
       int getSize() const;
       void push( const T& object );
       T pop();
    };
    ==============================

    stack.cpp

    Code:
    #include "include/stack.h"
    
    template<class T>
    Stack<T>::Stack(): top( 0 ), size( 0 ) {}
    
    template<class T>
    Stack<T>::~Stack() {
       while( top ) {
          StackNode * toDie = top;
          top = top->next;
          delete toDie;
       }
    }
    
    template<class T>
    int Stack<T>::getSize() const {
       return size;
    }
    
    template<class T>
    void Stack<T>::push( const T& object ) {
       top = new StackNode( object, top );
       ++size;
    }
    
    template<class T>
       T Stack<T>::pop() {
    
       StackNode * topOfStack = top;
       top = top->next;
    
       T data = topOfStack->data;
       delete topOfStack;
       --size;
    
       return data;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by ScottO
    Hello, I'm using VisualC++ version 6.0 to make a console application.

    Get the following error:
    sandbox.obj : error LNK2001: unresolved external symbol "public: __thiscall Stack<int>::~Stack<int>(void)" (??1?$Stack@H@@QAE@XZ)
    Copy and paste the contents of stack.cpp into stack.h and then, in sandbox.cpp you include stack.h. Because of the way templates are instantiated in the code, this doesn't lead to the same errors that you would get by defining a normal function in a header. This is because templates aren't compiled at the place of definition, but are compiled as they are used by code elsewhere.

    - xeddiex
    Last edited by xeddiex; 04-03-2006 at 12:18 AM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Ok, that makes sense. Can't compile a template ahead of time because you don't know what the type is going to be.

    I decided to just put the entire implementation of the template in a single .h file, and included that. Basically what you suggested. Works fine.

    Thanks xeddiex,

    Scott

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM
  2. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 02:01 PM
  3. Book's code: Problematic
    By RoD in forum Game Programming
    Replies: 14
    Last Post: 01-21-2003, 08:08 AM
  4. Help with error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-17-2002, 09:36 AM
  5. Compiler errors
    By BellosX in forum C Programming
    Replies: 2
    Last Post: 09-21-2001, 03:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21