我正在研究这个程序,但是我收到2个错误,我不知道它们是什么意思或我如何修复它们.... 程序应该读取任何等式为+ b-c或(a + b)-c ......等 并检查括号是否匹配 这是代码
// 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;
}
这是错误
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
答案 0 :(得分:0)
之前的答案因某种原因被删除了,所以我想我会发布一个。
正如你所说,equ当前的实现取决于要处理的类型是char,它会将签名更改为type&amp;没有意义,因为如果使用char类型实例化模板化类,它仍然只能按预期工作。然而,目前的问题是等号签名需要Stack&amp; amp;这是错误的,你应该改为char
template <typename type > void Stack <type> ::equal ( Stack& entry )
到
template <typename type > void Stack <type> ::equal ( char& entry )