-
Classes
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.
-
How have you included you header file? What is the code etc.
-
>> 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.
-
-
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:
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
{
++myTop;
myArray[myTop] = value;
} // or simply, myArray[++myTop] = value;
else
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
myTop--;
else
cerr << "*** Stack is empty -- can't remove a value ***\n";
}
My header file code:
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 *****/
public:
/* --- Constructor ---
*
* Precondition: A stack has been declared.
* Postcondition: The stack has been constructed as an
* empty stack.
************************************************************/
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 *****/
private:
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); }
#endif
-
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.
-
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.