I got some linker error, couldnt find the mistake. What is wrong?
#include "myStack.h" //Imports the Stack header file containing the declaration of the member functions
myStack::myStack()
/*The stack is initialized as empty */
{
count=0;
} //end of Stack::Stack constructor
Error_code myStack::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 myStack::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--;
return outcome;
} //end of Stack::pop function
Error_code myStack::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
return outcome;
} //end of Stack::top function
bool myStack::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
#ifndef STACK_H
#define STACK_H
typedef char Stack_entry;
enum Error_code{success,underflow,overflow};
/*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 myStack
{
public:
myStack(); //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 <iostream>
using namespace std;
#include "myStack.h"
int balanced( );
int main()
{
int state; //true=1 false=0
state = balanced(); //function that will decide if the brackets are balanced returns 1 or 0
return 0 ;
}
int balanced( )
{
char chr ; //character that will be tested one by one
char match ; //character that will be tested against the closing bracket as opening bracket
myStack stack ; //stack that will hold the brackets
chr = cin.get() ; //gets the first character
while ( (int) chr != 10 ) //continue if character is not empty
{
if ( chr == '(' || chr == '{' ) //if opening brackets then add stack
{
stack.push( chr ) ;
}
else if ( chr == ')' || chr == '}' ) //if closing brackets then look for the match in the stack
{
if ( stack.empty() == true )
{
cout << "Stack is empty. Brackets not matched!" << endl ;
return 0 ;
}
else
{
stack.top ( match ) ;
if( match == chr )
stack.pop() ;
else
{
cout << "The brackets did not match!" << endl ;
return 0 ;
}
}
}
chr = cin.get() ; //update the character
}
return 1 ;
}
1>------ Build started: Project: Manual Stack Implementation, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol "public: enum Error_code __thiscall myStack::top(char &)const " (?top@myStack@@QBE?AW4Error_code@@AAD@Z) referenced in function "int __cdecl balanced(void)" (?balanced@@YAHXZ)
1>main.obj : error LNK2019: unresolved external symbol "public: enum Error_code __thiscall myStack::push(char const &)" (?push@myStack@@QAE?AW4Error_code@@ABD@Z) referenced in function "int __cdecl balanced(void)" (?balanced@@YAHXZ)
1>C:\Users\Meltabi\Documents\Visual Studio 2008\Projects\Manual Stack Implementation\Debug\Manual Stack Implementation.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\Users\Meltabi\Documents\Visual Studio 2008\Projects\Manual Stack Implementation\Manual Stack Implementation\Debug\BuildLog.htm"
1>Manual Stack Implementation - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========