i am working on this program ,but i am receiving 2 errors , i cant know what they mean or how i fix them ….
the program should read any equation as a+b-c or (a+b)-c …etc
and check if the parenthesis match or not
here is the code
// A simple Integer stack, Array based
#include <string>
#include <iostream>
#define STACK_MAX 100
#define equation
using namespace std;
template <typename type>
class Stack {
private:
type data[STACK_MAX];
int size;
public:
Stack();
~Stack() { } // Destructor
int top();
void push(int d) ;
void pop() ;
int is_full ();
int is_empty () ;
void equal( Stack& entry) ;
};
template <typename type > int Stack<type>::is_full () {
return ( STACK_MAX >= size );
}
template <typename type > int Stack<type>::is_empty () {
return ( STACK_MAX == 0 );
}
template <typename type> Stack <type>::Stack() {
size = 0;
}
template <typename type > int Stack< type >:: top() {
if (is_empty ()) {
printf("Error: stack empty\n");
return -1;
}
return data[size-1];
}
template <typename type > void Stack <type>::push (int d) {
if (! is_full () ) {
data[size++] = d;
} else {
printf("Error: stack full\n");
}
}
template < typename type > void Stack <type>::pop() {
if (size == 0) {
printf("Error: stack empty\n");
} else {
size--;
}
}
template <typename type > void Stack <type> ::equal ( Stack& entry ) {
std::string& equation ;
char left = '(' ;
char right= ')' ;
Stack <type> x ;
string::size_type a ;
char next ;
next = entry ;
bool failed = false ;
for ( a= 0 ; ! equation.length() ; ++a) {
next = equation[a] ;
if ( next == left ) {
x.push (next);
} else {
if (x.is_empty()) {
x.pop () ;
} else {
failed = true ;
}
}
}
if ( a == 0) {
cout << " No parenthesis"<< endl ;
} else if ( x.top () == left ) {
cout << "Parenthesis don’t match. Missing right parenthesis" << endl ;
} else if ( x.top () == right ) {
cout << "Parenthesis don’t match. Missing left parenthesis " << endl ;
} else {
cout << "Matching parenthesis" << endl ;
}
}
int main() {
Stack <char> equation2 ;
char b ;
cout << " please enter your equation " << endl ;
for (int i =0 ; i< 100 ; i++ ) {
cin >> b ;
equation2.equal(b) ;
}
system ("pause") ;
return 0;
}
and here are the errors
2 IntelliSense: a reference of type "Stack<char> &" (not const-qualified) cannot be initialized with a value of type "char" c:\users\mike\documents\visual studio 2010\projects\stack\stack\stack.cpp 101
Error 1 error C2664: 'Stack<type>::equal' : cannot convert parameter 1 from 'char' to 'Stack<type> &' c:\users\mike\documents\visual studio 2010\projects\stack\stack\stack.cpp 101
The previous answer was for some reason deleted so I guess I’ll post one instead.
As you said, equal’s current implementation depends on the type to work on is a char, which would render changing the signature to type& quite meaningless because it would still only work as expected if you instantiated the templated class using the type char. Still, the current problem is that the equal signature takes a Stack& which is wrong, you should change to char
to