Thread: "error LNK2001: unresolved external symbol"problem

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Question "error LNK2001: unresolved external symbol"problem

    Code:
    compile is OK,but something wrong with link,I don't know what the matter. Help!
    The code  as  follows:
    
    
    //stack.h
    #include<iostream.h>
    template<class TYPE>
    class stack
    {public:
    stack(int s=10);
    ~stack()
    {delete [] stackptr;
    }
    int push(const TYPE&);
    int pop(TYPE&);
    int isFull() const
    {return top==size-1;
    }
    int isEmpty() const
    {return top==-1;
    }
    private:
           int size;
           int top;
           TYPE* stackptr;
    };
    
    
    //stack.cpp
    #include<iostream.h>
    #include"stack.h"
    template<class TYPE>
    stack<TYPE>::stack(int s)
    {size=s>0&&s<1000?s:10;
      top=-1;
      stackptr=new TYPE[size];
    }
    
    template<class TYPE>
    int stack<TYPE>::push(const TYPE& item)
    {if(!isFull())
    {stackptr[++top]=item;
    return 1;
    }
    return 0;
    }
    
    template<class TYPE>
    int stack<TYPE>::pop(TYPE& popvalue)
    {if(!isEmpty())
    {popvalue=stackptr[top--];
    return 1;
    }
    return 0;
    }
    
    
    //main.cpp
    #include<iostream.h>
    #include"stack.h"
    int main()
    {stack<char> charstack;
    char c='a';
    cout<<"pushing elements onto charstack"<<endl;
    while(charstack.push(c))
    {cout<<c<<" ";
    c+=1;
    }
    cout<<endl<<"stack is full.cannot push"<<c<<endl<<"poping elements from charstack"<<endl;
    while(charstack.pop(c))
    cout<<c<<" ";
    cout<<endl<<"stack is empty.cannot pop"<<endl;
    
    stack<float> floatstack(5);
    float f=1.1f;
    cout<<"pushing elements onto floatstack"<<endl;
    while(floatstack.push(f))
    {cout<<f<<" ";
    f+=1.1f;
    }
    cout<<endl<<"stack is full.cannot push"<<f<<endl<<"poping elements from floatstack"<<endl;
    while(floatstack.pop(f))
    cout<<f<<" ";
    cout<<endl<<"stack is empty.cannot pop"<<endl;
    
    stack<int> intstack;
    int i=1;
    cout<<"pushing elements onto intstack"<<endl;
    while(intstack.push(i))
    {cout<<i<<" ";
    i+=1;
    }
    cout<<endl<<"stack is full.cannot push"<<i<<endl<<"poping elements from intstack"<<endl;
    while(intstack.pop(i))
    cout<<i<<" ";
    cout<<endl<<"stack is empty.cannot pop"<<endl;
    return 0;
    }
    
    
    -------------------Configuration: aaa - Win32 Debug--------------------
    Compiling...
    stack.cpp
    main.cpp
    Linking...
    main.obj : error LNK2001: unresolved external symbol "public: int __thiscall stack<int>::pop(int &)" (?pop@?$stack@H@@QAEHAAH@Z)
    main.obj : error LNK2001: unresolved external symbol "public: int __thiscall stack<int>::push(int const &)" (?push@?$stack@H@@QAEHABH@Z)
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall stack<int>::stack<int>(int)" (??0?$stack@H@@QAE@H@Z)
    main.obj : error LNK2001: unresolved external symbol "public: int __thiscall stack<float>::pop(float &)" (?pop@?$stack@M@@QAEHAAM@Z)
    main.obj : error LNK2001: unresolved external symbol "public: int __thiscall stack<float>::push(float const &)" (?push@?$stack@M@@QAEHABM@Z)
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall stack<float>::stack<float>(int)" (??0?$stack@M@@QAE@H@Z)
    main.obj : error LNK2001: unresolved external symbol "public: int __thiscall stack<char>::pop(char &)" (?pop@?$stack@D@@QAEHAAD@Z)
    main.obj : error LNK2001: unresolved external symbol "public: int __thiscall stack<char>::push(char const &)" (?push@?$stack@D@@QAEHABD@Z)
    main.obj : error LNK2001: unresolved external symbol "public: __thiscall stack<char>::stack<char>(int)" (??0?$stack@D@@QAE@H@Z)
    Debug/aaa.exe : fatal error LNK1120: 9 unresolved externals
    执行 link.exe 时出错.
    
    aaa.exe - 1 error(s), 0 warning(s)

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Hello,

    That's a common problem . You've created the definition and implementation of the template in separate files, which is something C++ compilers barely tolerate. Put the implementation of the template together with the declaration in the header file; it should compile then.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read this SourceForge.net: Indentation - cpwiki

    Then perhaps more people will read your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM
  5. Unresolved external symbols in OGL
    By Shadow12345 in forum Game Programming
    Replies: 4
    Last Post: 08-04-2002, 09:46 PM