C++高手帮帮忙,看看为什么编译不过去呢
在头文件中Expr_node.h中,如下
#include<string>
#include<ostream.h>
class Expr_node;
class Int_node;
class Unary_node;
class Binary_node;
class Expr
{
friend ostream& operator<<(ostream& o,const Expr& t);
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& rhs)
{
rhs.p->use++;
if(--p->use == 0)
{
delete p;
p = NULL;
}
p = rhs.p;
return *this;
}
~Expr()
{
delete p;
}
};
class Expr_node
{
// friend ostream& operator << (ostream&,const Expr_node&);
friend class Expr;
int use;
public:
Expr_node():use(1){}
virtual void print(ostream& o)const =0;
virtual ~Expr_node(){}
};
//int 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;
}
};
// unary node
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<<")";
}
};
//Binary node
class Binary_node:public Expr_node
{
friend class Expr;
const 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;
}
在main.cpp中,如下
#include<iostream.h>
#include"Expr_node.h"
int main()
{
Expr t =Expr("*",Expr("-",5),Expr("+",3,4));
cout<<t<<endl;
t = Expr("*",t,t);
cout<<t<<endl;
return 0;
}
可是总也找不到原因为什么编译不过去
请大家帮忙看看,谢谢了