Thread: Undefined Reference

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    Undefined Reference

    I'm having problems with Undefined Reference. Posted below is my code:

    Code:
    /* Stack.h - Stack Specification */
    #include <stdlib.h>
    
    #ifndef STACK_H
    #define STACK_H
    struct StackNode
    {
       int entry;
       StackNode *next;
    
    };
    
    class Stack
    {
    	public:
    			Stack();    	
    			void push(int data);
       		int pop();
       		int peek();
       		bool IsEmpty();
    			void test();
     	private:	
    			StackNode *top;   
    			int counter; 
    
    };
    #endif
    and the Stack.cpp file:

    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include "Stack.h"
    
    Stack::Stack ()
    {
       counter = 0;
    	top = NULL;
    }
    
    void Stack :: push(int data)
    {
    	StackNode *temp = new StackNode;
    	temp -> entry = data;
    	temp -> next = top;
    	top = temp;
    	counter++;
    }
    
    int Stack :: pop()
    {
    	int dataHolder;
    	
    	dataHolder = top -> entry;
    	top = top -> next;
    	return dataHolder;
    }
    bool Stack :: IsEmpty()
    {
    	return (top == NULL);
    }
    
    int Stack :: peek()
    {
    	return top -> entry;
    }
    
    void Stack :: test()
    {
    	cout << "test";
    }
    and the Test.cpp with the main routine:

    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include "Stack.h"
    
    
    
    main ()
    {
    	Stack tester;
    	tester.test();	
    }

    I keep getting "undefined reference to `Stack::Stack(void)' " errors.
    I know I asked this in another thread of mine, but I didn't get much help... any help would be appreciated.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I'm not certain of why this isn't working. But just inline your Stack::Stack() and the problem will go away.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Quote Originally Posted by master5001
    I'm not certain of why this isn't working. But just inline your Stack::Stack() and the problem will go away.

    I wish I could but I can't... Could it be a problem with how I'm linking files together?

    Here is my make file:

    Code:
    # Makefile for Stack program
    
    all: 	main	
    
    main:	stdlib.h iostream.h Stack.h 
    	g++ Test.cpp Stack.cpp

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yes it is a problem with how its linking together, actually. I'll compile your code real quick and see if mine does the same thing.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It compiled for me just fine. Do a make clean or delete all the object files, maybe. Perhaps you have an object that was compiled early on and its mislinking.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Quote Originally Posted by master5001
    It compiled for me just fine. Do a make clean or delete all the object files, maybe. Perhaps you have an object that was compiled early on and its mislinking.

    This sounds stupid but... how did you compile it?

    I'm doing : "g++ Test.cpp Stack.cpp" and it compiles fine, but when I try to make some sort of executable out of it, I get the Undefined Reference error.

    Edit:

    Nevermind, I think I got something, thanks for your help.
    Last edited by xshapirox; 09-23-2004 at 01:22 PM.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    g++ *.cpp -o test

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM