Thread: Help with headers/linking code

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

    Help with headers/linking code

    Yeah, I'm having mad trouble getting my code to work together...

    when I compile this cpp file:

    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include "Stack.h"
    
    struct StackNode
    {
       int entry;
       StackNode *next;
    
    };
    
    Stack::Stack ()
    {
                    counter = 0;
    	top = NULL; //<---Here is where error two occurs
    }
    
    void Stack :: push(int data)
    {
    	StackNode temp;
    	temp.entry = data;
    	temp.next = top;
    	*top = temp;
    }
    
    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";
    }
    with this header:

    Code:
    /* Stack.h - Stack Specification */
    #include <stdlib.h>
    
    #ifndef STACK_H
    #define STACK_H
    
    class Stack
    {
    	public:
    	     Stack();    	
    			
                                    void push(int data);
       		int pop();
       		int peek();
       		bool IsEmpty();
    		void test();
     	private:	
    		StackNode *top;   //<<---here is where error
    		int counter;           //          one occurs
    
     };
    #endif
    I get these errors:

    Stack.h:17: syntax error before `*'
    Stack.cpp: In method `Stack::Stack()':
    Stack.cpp:36: `top' undeclared (first use this function)
    Stack.cpp:36: (Each undeclared identifier is reported only once
    Stack.cpp:36: for each function it appears in.)

    Nwhen I include the class definition in the cpp file, I get no errors, other than the fact that I don't have a main routine.

    Please help, as I have NOBODY around to help me out with this.

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You need to declare the StackStruct structure before the class declaration.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Quote Originally Posted by bithub
    You need to declare the StackStruct structure before the class declaration.
    You mean in the .h file?

    EDIT:

    Nevermind, it worked, thanks so much... I will be posting much more in this thread with questions relating to this program...

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    Ok, now I got another question... say I want to use that Stack.h /Stack.cpp(I think all I need to include is the .h file, right?)file in another .cpp file, how would I go about doing that?

    I tried including the Stack.h file, but got several errors. Once again here is my code and my errors (I'm just "testing" my .h file at this point so the code really doesn't do much):

    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include "Stack.h"
    
    
    
    main ()
    {
    	Stack tester;
    	tester.test();	
    }
    and the errors:
    /tmp/ccbuOX6v.o: In function `main':
    /tmp/ccbuOX6v.o(.text+0xe): undefined reference to `Stack::Stack(void)'
    /tmp/ccbuOX6v.o(.text+0x1d): undefined reference to `Stack::test(void)'
    collect2: ld returned 1 exit status


    Once again thanks for any help

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Make sure that you put Stack.cpp and Stack.h in the same project as the file that is using the stack.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    look for #ifndef and #define on google

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM