Thread: Classes

  1. #1
    Registered User
    Join Date
    Nov 2006


    I am learning classes and how to use them as header files. My question is, where do I put the header file? Do I simply save it in the same folder as the .cpp file? Right now I have tried putting my .cpp file AND my .h file in the same project. It's not working out.

  2. #2
    Registered User
    Join Date
    Nov 2005
    How have you included you header file? What is the code etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    >> Do I simply save it in the same folder as the .cpp file?
    Yes. Then you #include "header.h" in the source file (replace header.h with your header's filename). Adding it to the project is good but it has little effect on compilation.

    >> It's not working out.
    Post the errors you are getting.

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Read the FAQ.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!
    Share your experiences with us now!

    "We will game forever!"

  5. #5
    Registered User
    Join Date
    Nov 2006
    Still don't know what I am doing wrong.

    These are my errors:

    [Linker error] undefined reference to WinMain@16
    Id returned 1 exit status
    [Build error] [StackProject.exe] Error 1

    My main code:

    /* Stack.cpp -- implementation file for Stack.h */
    #include "Stack.h"
    #include <iostream>
    using namespace std;
    //--- Definition of push()
    void Stack::push(const StackElement & value)
      if (myTop < STACK_CAPACITY - 1)  // Preserve stack invariant
        myArray[myTop] = value;
      }                      // or simply, myArray[++myTop] = value;
        cerr << "*** Stack is full -- can't add new value ***\n"
             << "Must increase value of STACK_CAPACITY in Stack.h\n";
    //--- Definition of display()
    void Stack::display(ostream & out) const
      for (int i = myTop; i >= 0; i--) 
        out << myArray[i] << endl;
    //--- Definition of top()
    StackElement Stack::top() const
      if (myTop >= 0)
        return myArray[myTop];
      cerr << "*** Stack is empty ***\n";
    //--- Definition of pop()
    void Stack::pop()
      if (myTop >= 0)    // Preserve stack invariant
        cerr << "*** Stack is empty -- can't remove a value ***\n";
    My header file code:
    /* Stack.h provides a Stack class.
     * Basic operations:
     *   Constructor:  Constructs an empty stack
     *   empty:   Checks if a stack is empty
     *   push:    Modifies a stack by adding a value at the top
     *   top:     Accesses the top stack value; leaves stack unchanged
     *   pop:     Modifies a stack by removing the value at the top
     *   display: Displays all the stack elements
     * Class Invariant:
     *   1. The stack elements (if any) are stored in positions
     *      0, 1, . . ., myTop of myArray.
     *   2. -1 <= myTop < STACK_CAPACITY 
    #include <iostream>
    using namespace std;
    #ifndef STACK
    #define STACK
    const int STACK_CAPACITY = 128;
    typedef int StackElement;
    class Stack
    /***** Function Members *****/
    /* --- Constructor ---
     * Precondition:  A stack has been declared.
     * Postcondition: The stack has been constructed as an 
     *                 empty stack.
    /* --- Is the Stack empty? ---
     * Receive: Stack containing this function (implicitly)
     * Returns: True iff the Stack containing this function is empty
    bool empty() const;
    /* --- Add a value to the stack ---
     * Receive: The Stack containing this function (implicitly)
     *          A value to be added to a Stack
     * Return:  The Stack (implicitly), with value added at its 
     *          top, provided there's space
     * Output:  "Stack full" message if no space for value
    void push(const StackElement & value);
    /* --- Display values stored in the stack ---
     * Receive: The Stack containing this function (implicitly)
     *          The ostream out
     * Output:  The Stack's contents, from top down, to out
    void display(ostream & out) const;
    /* --- Return value at top of the stack ---
     * Receive: The Stack containing this function (implicitly)
     * Return:  The value at the top of the Stack, if nonempty;
     *          else a "garbage value"
     * Output:  "Stack empty" message if stack is empty
    StackElement top() const;
    /* --- Remove value at top of the stack ---
     * Receive: The Stack containing this function (implicitly)
     * Return:  The Stack containing this function (implicitly)
     *           with its top value (if any) removed
     * Output:  "Stack-empty" message if stack is empty.
    void pop();
    /***** Data Members *****/
      StackElement myArray[STACK_CAPACITY];
      int myTop;
    }; // end of class declaration
    //--- Definition of Class Constructor
    inline Stack::Stack() 
    { myTop = -1; } 
    //--- Definition of empty
    inline bool Stack::empty() const
    { return (myTop == -1); } 

  6. #6
    Registered User
    Join Date
    Jan 2005
    That error is not related to your handling of the header and source file, you are doing that correctly. That error is saying that there is no main function in your program. When you build a project, it compiles all the source files first (that succeeded for you), then it links them together to create an executable. That executable needs a main function to start, but if you haven't provided one then you get an error looking for main.

    To fix this you can make an empty main that does nothing, or put some test code inside a main function, or just wait until you are ready to code the main function and just compile (instead of build) until then.

    One other issue you have is your project type. It looks like you created a Windows Application project instead of a Console Application project. I'm not sure what IDE you have, but you need to indicate that you want a console application instead of a Windows application.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Thanks for your reply. I didn't use a windows app, I used the console app. But my problem was fixed when I went back and added a main function. Thank you for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM