I have tried to implement a stack class but i got stupid errors. There is a stupid error i know but i couldnt find it.

#ifndef STACK_H
#define STACK_H

/*Manual implementation of Stack class.
  By this way it will be possible to learn the advantages of Stack 
  data structure over others. This is the header file of class Stack */

const int maxstack=10; //A small value to be tested as a limit of the stack

class Stack
{
public:
	Stack(); //Constructor
	bool empty() const; //constant function returns true if the stack is empty otherwise returns false
	
	Error_code pop(); /*Function tries to pop the value from the stack if the stack is not empty
					  and returns Error_code*/

	Error_code push(const Stack_entry &item); /*function tries to push the value of constant item to 
											  the top of the stack and returns Error_code*/

	Error_code top(Stack_entry &item) const; /*function tries to copy the value of the top of the
											 stack into the item and returns Error_code */

private:
	int count; // counts the number of values existing in the stack

	Stack_entry entry[maxstack]; //the decleration of the stack that will hold the values.
};

#endif
#include "stack.h" //Imports the Stack header file containing the declaration of the member functions

Stack::Stack()
/*The stack is initialized as empty */
{
	count=0;
} //end of Stack::Stack constructor

Error_code Stack::push(const Stack_entry &item)
/*If the stack is full then the error code overflow is returned
else the item is pushed to the top of the stack and return error code success */
{
	Error_code outcome=success; //default value of outcome is set to success

	if(count>=maxstack) //the overflow condition is tested
		outcome=overflow;
	
	else //the success condition will be implemented
		entry[count++]=item;

	return outcome; //returns the Error_code type outcome

} //end of Stack::push function

Error_code Stack::pop()
/*If the stack is empty then return error code underflow and do nothing
else if the stack is not emtpy then pop the top of the stack */
{
	Error_code outcome=success; //the default value of outcome is set to success

	if(count==0) //stack is empty case is handled
		outcome==underflow;

	else //stack is not empty case is handled and pop is executed
		count--;

} //end of Stack::pop function

Error_code Stack::top(Stack_entry &item) const
/*If the stack is empty then an error of underflow is returned.
Else if the stack is not empty then the top of the stack is assigned to the item */
{
	Error_code outcome=success; //the default value of outcome is set to success

	if(count==0) //the stack if empty case is handled and returned the underflow error code
		outcome=underflow;

	else /*the stack is not empty case is handled and the value of the top of the stack is assigned
		 to the parameter item */

		item=entry[count-1]; //count-1 represents the top of the stack

} //end of Stack::top function

bool Stack::empty() const
/* If the stack is empty then the function returns true
else if the stack is not empty then the function returns false */
{
	bool outcome=false; //the default value of returning value outcome is set to false
	
	if(count==0) //the case that the stack is empty is handled
		outcome=true;

	return outcome; //outcome is returned

} //end of Stack::empty function
typedef double Stack_entry;
#include "stack.h"

enum Error_code{success,underflow,overflow};

int main()
{}
1>------ Build started: Project: Manual Stack Implementation, Configuration: Debug Win32 ------
1>Compiling...
1>Stack.c
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(10) : error C2061: syntax error : identifier 'Stack'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(10) : error C2059: syntax error : ';'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(11) : error C2449: found '{' at file scope (missing function header?)
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(29) : error C2059: syntax error : '}'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(9) : error C2061: syntax error : identifier 'Stack'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(9) : error C2059: syntax error : ';'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(9) : error C2059: syntax error : ':'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(25) : error C2061: syntax error : identifier 'Stack'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(25) : error C2059: syntax error : ';'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(25) : error C2059: syntax error : ':'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(39) : error C2061: syntax error : identifier 'Stack'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(39) : error C2059: syntax error : ';'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(39) : error C2059: syntax error : ':'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(55) : error C2061: syntax error : identifier 'Stack'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(55) : error C2059: syntax error : ';'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.c(55) : error C2059: syntax error : ':'
1>main.c
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(10) : error C2061: syntax error : identifier 'Stack'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(10) : error C2059: syntax error : ';'
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(11) : error C2449: found '{' at file scope (missing function header?)
1>c:\users\mert\documents\visual studio 2008\projects\manual stack implementation\manual stack implementation\stack.h(29) : error C2059: syntax error : '}'
1>Generating Code...
1>Build log was saved at "file://c:\Users\Mert\Documents\Visual Studio 2008\Projects\Manual Stack Implementation\Manual Stack Implementation\Debug\BuildLog.htm"
1>Manual Stack Implementation - 20 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Recommended Answers

All 2 Replies

Considering your filenames, you are probably compiling as C instead of C++. C does not support classes, only C++ does. You will need to change your source filenames from (*.c) to (*.cpp).

Also, try changing your class name and file name to something else such as "myStack" or "manualStack". Once the compiler is corrected, you're header file and class definition are probably going to conflict with the STL stack class that is part of your compiler and confuse it.

Thanks, lol I gave the extension .c as it was in the book. That will solve the problem i'm sure.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.