首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++初学者一个编程有关问题

2013-07-01 
C++菜鸟求助一个编程问题《C++沉思录》中第8张中的问题:实现一个表达式树的打印我实现了一遍,但是总是编译不

C++菜鸟求助一个编程问题
《C++沉思录》中第8张中的问题:实现一个表达式树的打印
我实现了一遍,但是总是编译不过
请教问题何在

代码如下:
#include <iostream>
#include <string>

using namespace std;
class Expr_node;

class Expr{
friend ostream & operator<<(ostream &, const Expr&);
public:
Expr_node *p;

public:
Expr(int);
Expr(const string&, Expr);
Expr(const string&, Expr, Expr);
Expr(const Expr& t);
Expr& operator=(const Expr&);
~Expr();
};

class Expr_node{
 friend Expr;
 int use;

public:
 Expr_node():use(1){}
 virtual void print(ostream &)const =0;
 virtual ~Expr_node(){}; 
};
 
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<<")";}
};

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 left, Expr right){
p = new Binary_node(op, left, right);
}

Expr& Expr::operator =(const Expr& rhs){
rhs.p->use++;
if(--p->use == 0){
delete p;
}
p = rhs.p;
return *this;
}

Expr::Expr(const Expr& t){
p = t.p; ++p->use;
}

Expr::~Expr(){
    if(--p->use==0) delete p;
}

ostream &operator<<(ostream &o, const Expr&t){
t.p->print(o);
return o;
}

int main(){
Expr t = Expr("*", Expr("-", 5), Expr("+", 3, 4));


cout<<t<<endl;
t = Expr("*", t, t);
cout<<t<<endl;

return 0;
}
求高手解答
我这里能编译通过啊,vs2008
LZ用的什么编译环境哦!啥子问题?
[解决办法]
错误提示给出来啊。
[解决办法]
我表示我看都不想看,太长了……
[解决办法]
是不是环境问题?

热点排行