代码出错在哪?

fyhack 2010-04-27 11:02:12
这是C++沉思录的一个例子,但调试总有错误,大家帮忙看看


//#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;
}
...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2010-04-30
  • 打赏
  • 举报
回复
String为vcl内建类型,要改为string.LZ还漏了一些代码.

以下是修改后可在VC6下可编译运行的程序:
#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;
}

wade_2003 2010-04-30
  • 打赏
  • 举报
回复
Int_node,Unary_node....怎么都没有定义呢?
fyhack 2010-04-29
  • 打赏
  • 举报
回复
还是没有解决,哪位朋友在VC6.0下编译通过 发个源码啊
arong1234 2010-04-27
  • 打赏
  • 举报
回复
至少两个问题:
1.
Expr& operator=(const Expr& t)
{
(t.p->use)++;
if(--(p->use)==0) delete p;
p=t.p;
return *this;
}

这个代码,this和t的p指向同一个内存,这必然造成指针悬挂,当this和t都被析构时,p会被delete至少2次。p=t.p时不合法的,你需要给this->p分配内存空间,然后把t.p的内容复制过去

2. 你又有friend又有std,在VC6里这是知名bug,肯定编译不过去
z569362161 2010-04-27
  • 打赏
  • 举报
回复
太长了

64,661

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧