[RESOLVED] error LNK2019: unresolved external symbol???
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] error LNK2019: unresolved external symbol???

  1. #1
    Join Date
    Sep 2009
    Posts
    7

    [RESOLVED] error LNK2019: unresolved external symbol???

    I don't know why I'm getting this error, this is a post fix calculator for array use review, I'll include only the push functions to save post space. If i comment out the push/pops, the errors does not occur.

    The fatal errors:
    Code:
    error LNK2019: unresolved external symbol "public: void __thiscall stack::pop(int &)" (?pop@stack@@QAEXAAH@Z) referenced in function _main
    1>client.obj : error LNK2019: unresolved external symbol "public: void __thiscall stack::push(int)" (?push@stack@@QAEXH@Z) referenced in function _main
    The client program (part of)
    Code:
    stack myStack;
    int arg1;  // this will hold the left operand.
    int arg2; //this will hold the right operand.
    int result; // result of every calculation, to be pushed onto stack.
    int m;
    
    int main()
    {
    	string s1;
    	cout << "Input the numbers followed by operator(s): " << endl;
    	cin >> s1;
    	
    	for (int i=0;s1[i]!='\0';i++) 
    	{
    		if (isdigit(s1[i])){  //used isdigit to check if number, if true; push.
    			m = (int)s1[i];
    			cout << m << endl;
    		myStack.push(m); 
    		}else if(s1[i]=='+'){
    			myStack.pop(arg1); //pops operand off top of stack and into arg1 which is a int data
    ...........................................
    The stack.h and stack.cpp PUSH code:

    Code:
    typedef int el_t;    // the el_t type is int for now
                         // el_t is unknown to the client
    
    
    class stack
      { 
    
       private: // to be hidden from the client
    
            el_t     el[MAX];       // el is  an array of el_t's --int's
    
    .........................
    
    		void push(el_t);
    
    ......................cpp below
    
    void stack::push(el_t n)  
    {
      if (isFull())
    	  throw Overflow();
      else{
    	  top++;
    	  el[top] = n;
      }
    }

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

    Re: error LNK2019: unresolved external symbol???

    Looks like you forgot to add the implementation file for stack to your project.
    Kurt

  3. #3
    Join Date
    Sep 2009
    Posts
    7

    Smile Re: error LNK2019: unresolved external symbol???

    Quote Originally Posted by ZuK View Post
    Looks like you forgot to add the implementation file for stack to your project.
    Kurt
    Implementation file is the stack.cpp file mentioned above with the code:

    Code:
    void stack::push(el_t n)  
    {
      if (isFull())
    	  throw Overflow();
      else{
    	  top++;
    	  el[top] = n;
      }
    }
    (and more but only included what I thought would be relevant.

    That is, if I understand what an 'implementation file' is...

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

    Re: error LNK2019: unresolved external symbol???

    Yes. stack.cpp is the implementation file for the stack class.
    As long as you're not posting compilable code, all I can do is guessing.
    Kurt

  5. #5
    Join Date
    Sep 2009
    Posts
    7

    Smile Re: error LNK2019: unresolved external symbol???

    Compilable code:

    Stack.h
    Code:
    int const MAX = 10;   // The MAX number of elements for the stack
                         // MAX is unknown to the client
    				// MAX is to prevent double digit numbers.
    
    typedef int el_t;    // the el_t type is int for now
                         // el_t is unknown to the client
    
    
    class stack
      { 
    
       private: // to be hidden from the client
    
            el_t     el[MAX];       // el is  an array of el_t's --int's
            int      top;           // top is index to the top of stack
    	void stackError(char*); // utility function for error handling
    
       public: // prototypes to be used by the client
    
          	stack();   // constructor
          	~stack();  // destructor to get rid of object
    
    		class Overflow{};  //exception to be thrown  //"withing stack class"???
    		class Underflow{};  //exception to be thrown
    
            // How to call: format stackname.push(argument)
            // PURPOSE: if not full, enters an element at the top
    		void push(el_t);
    
            // How to call: format stackname.push(pass by reference argument)
            // PURPOSE: if not empty, removes and gives back the top element
            void pop(el_t&);  //Pass by reference, get element back as arg
    
            // HOW TO CALL: stackname.push(pass by reference argument)
            // PURPOSE: if not empty, gives the top element without removing it
            void topElem(el_t&); //Pass by reference, get element back as arg
    
     		// How to call:  stackname.isEmpty();
    		// PURPPOSE: returns true is stack is empty, else false  
          	bool isEmpty();  
    
    		// How to call:  stackname.isFull();
    		// PURPPOSE: returns true if stack is full, else false
          	bool isFull();	  
    
    		// How to call:  stackname.displayAll();
    		// PURPPOSE: display all elements in el_t, vertically.  Non distructive.
            void displayAll();
    		// How to call:  stackname.clearIt();
    		// PURPPOSE: Clear the entire stack.
            void clearIt();
            
      };  
    
    // Note: semicolon is needed at the end of the header file
    stack.cpp:
    Code:
    #include <iostream>
    using namespace std;
    
    #include "stack.h"
    
    
    // PURPOSE: constructor which initializes top
    stack::stack()
    {
      top = -1;
    }
    
    
    // PURPOSE: destructor which does nothing for the static array
    stack::~stack() {
      /* nothing to do since this is a static array */ 
    }
    
    
    // PURPOSE: (private) to handle unexpected errors encountered by other methods
    // PARAMS:  a string message to be displayed
    // ALGORITHM: simply cout the message and exit from the program
    void stack::stackError(char* msg)
    { cout << msg << endl; exit(1); }
    
    
    
    // PURPOSE: (public) to push element onto top of el[] stack if not full.
    // PARAMS:  A number to be checked then pushed
    // ALGORITHM: use isFull to if stack is full, 
    	//if not increase top by 1 and copy n to that new spot.
    void stack::push(el_t n)  
    {
      if (isFull())
    	  throw Overflow();
      else{
    	  top++;
    	  el[top] = n;
      }
    }
    
    // PURPOSE: (public) to pop element off of top of el[] stack if not empty.
    // PARAMS:  A number to be checked then pushed
    // ALGORITHM: use isEmpty to if stack is empty, 
    	//if not; copy top spot to n and decrease top by 1.
    void stack::pop(el_t& n) 
    {
      if (isEmpty())
    	  throw Underflow();
      else {
    	  n = el[top];
    	  top--;
      }
    }
    
    
    // PURPOSE: (public) to give top element of stack if not empty
    // PARAMS:  argument to be checked 
    // ALGORITHM: calls isEmpty and if true, throws underflow exception
    	//else, gives back top element of el.
    void stack::topElem(el_t& n) 
    {
    	if(isEmpty())
    		throw Underflow();
    	else 
    		n = el[top];
    }
    
    
    // PURPOSE: (public) checks stack to see if elements do not exist exist (empty)
    // PARAMS:  returns bool.
    // ALGORITHM: checks top, if top <0, stack is empty, return true.  Else false.
    bool stack::isEmpty() {
      if (top==-1)
    	  return true;
      else
    	  return false;
    }
    
    
    // PURPOSE: (public) checks stack to see if 10 single digit elements exist (full)
    // PARAMS:  returns bool.
    // ALGORITHM: checks top, if top >9, stack is full, return true.  Else false.
    bool stack::isFull() {
      if (top==MAX-1)
    	  return true;
      else
    	return false;
    }
    
    
    // PURPOSE: (public) displays all existing elements of stack.
    // PARAMS:  None.
    // ALGORITHM: calls isEmpty.  If false displays elements vertically. If true displays '[empty]
    void stack::displayAll() 
    {
      if (isEmpty()) cout << "[ empty ]" <<endl;
      else
        for (int i = top; i >= 0; i--)
          {  cout << el[i] << "\n" << endl; }
    }
    
    // PURPOSE: (public) Clears the stack of elements if not already empty. 
    // PARAMS:  None. Void.
    // ALGORITHM: while not isEmpty, keep popping the stack
    void stack::clearIt()
    {
      while(!isEmpty())
    	  pop(el[top]);
    }// the end
    client.cpp
    Code:
    #include <stdlib.h> 
    #include <iostream>
    #include <string>
    #include <sstream>
    #include "stack.h"
    
    using namespace std;
    
    stack myStack;
    int arg1;  // this will hold the left operand.
    int arg2; //this will hold the right operand.
    int result; // result of every calculation, to be pushed onto stack.
    int m;
    
    int main()
    {
    	string s1;
    	cout << "Input the numbers followed by operator(s): " << endl;
    	cin >> s1;
    	
    	for (int i=0;s1[i]!='\0';i++) 
    	{
    		if (isdigit(s1[i])){  //used isdigit to check if number, if true; push.
    			m = (int)s1[i];
    			cout << m << endl;
    		myStack.push(m); 
    		}else if(s1[i]=='+'){
    			myStack.pop(arg1); //pops operand off top of stack and into arg1 which is a int data
    			myStack.pop(arg2); //pops operand off top of stack and into arg2 which is a int data
    			result = arg1 + arg2;
    			cout << "arg1 and arg2 and result: " << arg2 << " " << arg1 << " " << (int)result-48 << endl;
    			myStack.push((int)result-48); //HOW TO INPUT RESULT?????
    		} else
    			cout << "else called from client" << endl;
    			
    	}
    
    	myStack.displayAll();
    
    
    /*  WORKING BELOW
    if(!myStack.isFull())
    	myStack.push('A');
    //else
    //throw OverFlow();
    myStack.displayAll();
    char C;
    if (!myStack.isEmpty())
    	myStack.pop(C);  //pop INTO C
    //else
    //throw UnderFlow();
    myStack.displayAll();
    
    cout << C << endl;
    */
    	
    system("PAUSE");
    
    }

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

    Re: error LNK2019: unresolved external symbol???

    Compiles fine for me.
    So my first statement holds. Did you add stack.cpp to the project ?
    Kurt

  7. #7
    Join Date
    Sep 2009
    Posts
    7

    Smile Re: error LNK2019: unresolved external symbol???

    Quote Originally Posted by ZuK View Post
    Compiles fine for me.
    So my first statement holds. Did you add stack.cpp to the project ?
    Kurt
    resolved. Good call, ZuK, for turning my attention to Visual Studio.

    I copied all the source files to the local folder after working on the project from a different location, on a thumb drive. I also changed the stack.cpp file name. Resolved by compiling stack.cpp before compiling client.

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center