回 帖 发 新 帖 刷新版面

主题:[讨论]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)



各位高手给指点下啊  先谢谢了~~~~~~~~~~~~·

回复列表 (共2个回复)

沙发

:\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
问题出在你在类中将类名stack又定义为变量名:T*stack,改个名即可

d:\program files\microsoft visual studio\myprojects\kuo\main.cpp(17) : error C2137: empty character constant
问题出在cout<<j<<''<<i<<endl;改为cout<<j<<" "<<i<<endl;


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 时出错.
问题在于OutOfBounds是个异常类,你的工程中没有包含此类的文件

板凳

我把程序修改了下 可是还是有两个错误啊

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()
    {
        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);
}


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())return false;
    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())return false;
    x=stack[top--];
    return *this;
}


--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
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 时出错.

main.exe - 1 error(s), 0 warning(s)

我来回复

您尚未登录,请登录后再回复。点此登录或注册