主题:[讨论]C++执行.exe时出错 大家帮忙看看啊
这个是 stack.h中的
template<class T>
class stack
{
public:
stack(int MaxStackSize=10);
~stack(){Delete[]stack;}
bool isempty()const{return top==-1;}
bool isfull()const{return top==maxtop;}
T Top()const;
stack<T>& add(const T& x);
stack<T>& Delete(T& x);
private:
int top;
int maxtop;
T *stack;
};
template<class T>
stack<T>::stack(int MaxStackSize)
{
maxtop=MaxStackSize-1;
stack=new T[MaxStackSize];
top=-1;
}
template<class T>
T stack<T>::Top()const
{
if(isempty())throw OutOfBounds();
else return stack[top];
}
template<class T>
stack<T>& stack<T>::add(const T& x)
{
if(isfull())throw nomem();
stack[++top]=x;
return *this;
}
template<class T>
stack<T>& stack<T>::Delete(T& x)
{
if(isempty())throw OutOfBounds();
x=stack[top--];
return *this;
}
这个是main.cpp中的
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include"stack.h"
const int MaxLength=100;
void PrintMatchedPairs(char *expr)
{
stack<int>s(MaxLength);
int j,length=strlen(expr);
for(int i=1;i<=length;i++)
{
if(expr[i-1]=='(')s.add(i);
else if(expr[i-1]==')')
try
{
s.Delete(j);
cout<<j<<''<<i<<endl;
}
catch(OutOfBounds)
{
cout<<"no match for right parenthesis"<<"at"<<i<<endl;
}
}
while(!s.isempty())
{
s.Delete(j);
cout<<"no match for left parenthesis at"<<j<<endl;
}
}
void main(void)
{
char expr[MaxLength];
cout<<"type an expression of length at most"<<MaxLength<<endl;
cin.getline(expr,MaxLength);
cout<<"the pairs of matching parenthesis in"<<endl;
puts(expr);
cout<<"are"<<endl;
PrintMatchedPairs(expr);
}
可是编译执行.exe的时候出现以下错误
--------------------Configuration: kuo - Win32 Debug--------------------
Compiling...
main.cpp
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : error C2580: redefinition of class name 'stack<`template-parameter257'>'
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : see declaration of 'stack<`template-parameter257'>'
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(16) : see reference to class template instantiation 'stack<T>' being compiled
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : error C2580: redefinition of class name 'stack<int>'
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : see declaration of 'stack<int>'
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(8) : see reference to class template instantiation 'stack<int>' being compiled
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(17) : error C2137: empty character constant
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(19) : error C2061: syntax error : identifier 'outofbounds'
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(19) : error C2310: catch handlers must specify one type
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(23) : error C2317: 'try' block starting on line '15' has no catch handlers
执行 cl.exe 时出错.
kuo.exe - 1 error(s), 0 warning(s)
各位高手给指点下啊 先谢谢了~~~~~~~~~~~~·
template<class T>
class stack
{
public:
stack(int MaxStackSize=10);
~stack(){Delete[]stack;}
bool isempty()const{return top==-1;}
bool isfull()const{return top==maxtop;}
T Top()const;
stack<T>& add(const T& x);
stack<T>& Delete(T& x);
private:
int top;
int maxtop;
T *stack;
};
template<class T>
stack<T>::stack(int MaxStackSize)
{
maxtop=MaxStackSize-1;
stack=new T[MaxStackSize];
top=-1;
}
template<class T>
T stack<T>::Top()const
{
if(isempty())throw OutOfBounds();
else return stack[top];
}
template<class T>
stack<T>& stack<T>::add(const T& x)
{
if(isfull())throw nomem();
stack[++top]=x;
return *this;
}
template<class T>
stack<T>& stack<T>::Delete(T& x)
{
if(isempty())throw OutOfBounds();
x=stack[top--];
return *this;
}
这个是main.cpp中的
#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include"stack.h"
const int MaxLength=100;
void PrintMatchedPairs(char *expr)
{
stack<int>s(MaxLength);
int j,length=strlen(expr);
for(int i=1;i<=length;i++)
{
if(expr[i-1]=='(')s.add(i);
else if(expr[i-1]==')')
try
{
s.Delete(j);
cout<<j<<''<<i<<endl;
}
catch(OutOfBounds)
{
cout<<"no match for right parenthesis"<<"at"<<i<<endl;
}
}
while(!s.isempty())
{
s.Delete(j);
cout<<"no match for left parenthesis at"<<j<<endl;
}
}
void main(void)
{
char expr[MaxLength];
cout<<"type an expression of length at most"<<MaxLength<<endl;
cin.getline(expr,MaxLength);
cout<<"the pairs of matching parenthesis in"<<endl;
puts(expr);
cout<<"are"<<endl;
PrintMatchedPairs(expr);
}
可是编译执行.exe的时候出现以下错误
--------------------Configuration: kuo - Win32 Debug--------------------
Compiling...
main.cpp
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : error C2580: redefinition of class name 'stack<`template-parameter257'>'
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : see declaration of 'stack<`template-parameter257'>'
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(16) : see reference to class template instantiation 'stack<T>' being compiled
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : error C2580: redefinition of class name 'stack<int>'
d:\program files\microsoft visual studio\myprojects\kuo\stack.h(15) : see declaration of 'stack<int>'
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(8) : see reference to class template instantiation 'stack<int>' being compiled
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(17) : error C2137: empty character constant
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(19) : error C2061: syntax error : identifier 'outofbounds'
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(19) : error C2310: catch handlers must specify one type
d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(23) : error C2317: 'try' block starting on line '15' has no catch handlers
执行 cl.exe 时出错.
kuo.exe - 1 error(s), 0 warning(s)
各位高手给指点下啊 先谢谢了~~~~~~~~~~~~·