读书人

代码出错在哪?解决方案

发布时间: 2012-03-07 09:13:51 作者: rapoo

代码出错在哪?
这是C++沉思录的一个例子,但调试总有错误,大家帮忙看看

C/C++ code
//#include "stdafx.h"#include <iostream>#include <string>using namespace std;//节点类class Expr_node{    friend ostream& operator<<(ostream&,const Expr_node&);    friend class Expr;    int use;protected:    Expr_node()    {        use=1;    }    virtual void print(ostream&) const =0;                    //节点的打印    virtual ~Expr_node(){}};class Expr{    friend ostream& operator<<(ostream&,const Expr&);    Expr_node* p;public:    Expr(int n)    {        p=new Int_node(n);    }     Expr(const String& op,Expr t)    {        p=new Unary_node(op,t);    }    Expr(const String& op,Expr left,Expr right)    {        p=new Binary_node(op,left,right);    }    Expr(const Expr& t)    {        p=t.p;        ++(p->use);    }    Expr& operator=(const Expr& t)    {        (t.p->use)++;        if(--(p->use)==0)    delete p;        p=t.p;        return *this;    }};ostream& operator<<(ostream& o,const Expr& t){    t.p->print(o);    return o;}class Int_node :public Expr_node{    friend class Expr;    int n;    Int_node(int k)    {        n=k;    }    void print(ostream& o)  const    {        o<<n;    }};class Unary_node :public Expr_node{    friend class Expr;    String op;    Expr opnd;    Unary_node(const String& a,Expr b)    {        op=a;        opnd=b;    }    void print(ostream& o) const    {        o<<"("<<op<<opnd<<")";    }};class Binary_node :public Expr_node{    friend class Expr;    String op;    Expr left;    Expr right;    Binary_node(const String& a,Expr b,Expr c)    {        op=a;        left=b;        right=c;    }    void print(ostream& o) const    {        o<<"("<<left<<op<<right<<")";    }};void main(){    Expr t=Exp("*",Expr("-",5),Expr("+",3,4));    cout<<t<<endl;}


[解决办法]
String为vcl内建类型,要改为string.LZ还漏了一些代码.

以下是修改后可在VC6下可编译运行的程序:
C/C++ code
#include <iostream>#include <string>using namespace std;class Expr {    friend ostream& operator << ( ostream&, const Expr& );    friend class  Expr_node;    Expr_node *p;public:    Expr (int n);    Expr (const string& op, Expr t );    Expr (const string& op , Expr r, Expr l);    Expr (const Expr &);    Expr& operator = (const Expr &);    ~Expr ( ) ;};class Expr_node{    friend ostream& operator << ( ostream& os, const Expr& rhs);    friend ostream& operator << ( ostream&, const Expr_node& );    friend class Expr;    int use;protected:    Expr_node():use(1){}    virtual void print ( ostream& )const = 0;    virtual ~Expr_node ( ){}};ostream& operator << (ostream& o, const Expr_node& t){    t.print (o);    return o;}class Int_node : public Expr_node{    friend class Expr;    int n;    Int_node ( int k ): n(k) { }    void print( ostream& o) const     {         o<<n;    }};    class Unary_node : public Expr_node {    friend class Expr;    string op;    Expr opnd;    Unary_node (const string& a, Expr b): op (a), opnd(b) { }    void print ( ostream& o) const     {         o<<"("<<op<< opnd<<")";    }};class Binary_node:public Expr_node{    friend class Expr;    string op;    Expr left;    Expr right;    Binary_node(const string& a,Expr b,Expr c):        op (a), left (b),right(c) {}     void print(ostream& o) const    {        o<<"("<<left<<op<<right<<")";    }};ostream& operator << ( ostream& o, const Expr& t){    t.p->print (o);    return o;}Expr::Expr ( const Expr& t){    p=t.p;    ++(p->use);}Expr& Expr::operator = ( const Expr& t){    (t.p->use)++;    if(--(p->use)==0)    delete p;    p=t.p;    return *this;}Expr::Expr(int n ) {p = new Int_node (n);}Expr::Expr(const string& op, Expr t) { p=new Unary_node(op,t);}Expr::Expr(const string& op, Expr r, Expr l) {p = new Binary_node (op, r,l);}Expr::~Expr( ) {if (--p->use ==0) delete p;}                                                       void main(){    Expr t=Expr("*",Expr("-",5),Expr("+",3,4));    cout<<t<<endl;} 

读书人网 >C++

热点推荐