急!!!新手遇到编译错误。(解决立即结贴)

falcon6666 2004-12-15 08:44:20
我笨死了。
找了一个多小时,硬是没找出错在哪儿。请大虾指正。

bcb6.0

代码如下:
#include <iostream>
#include <string>
using namespace std;

class Expr;
class Expr_node
{
friend ostream& operator << (ostream &, const Expr_node&);
friend class Expr;
int use;
//private:
protected:
Expr_node(): use( 1 ) {}
virtual void print(ostream &) const = 0;
virtual ~Expr_node() {}

};

ostream&
operator << (ostream &o, const Expr_node& e)
{
e.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 << ")"; }

};

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

public:
Expr( int );
Expr( const string &, Expr );
Expr( const string &, Expr, Expr );
Expr( const Expr &t)
{ p = t.p; ++p->use; }
Expr & operator = ( const Expr &t );
~Expr() { delete p; }
};

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;
}

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;
}


错误提示如下:
Build
[C++ Error] a1.cpp(37): E2450 Undefined structure 'Expr'
[C++ Error] a1.cpp(37): E2449 Size of 'opnd' is unknown or zero
[C++ Error] a1.cpp(37): E2450 Undefined structure 'Expr'
[C++ Error] a1.cpp(49): E2450 Undefined structure 'Expr'
[C++ Error] a1.cpp(49): E2449 Size of 'left' is unknown or zero
[C++ Error] a1.cpp(49): E2450 Undefined structure 'Expr'
[C++ Error] a1.cpp(50): E2450 Undefined structure 'Expr'
[C++ Error] a1.cpp(50): E2449 Size of 'right' is unknown or zero
[C++ Error] a1.cpp(50): E2450 Undefined structure 'Expr'
...全文
108 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
falcon6666 2004-12-15
  • 打赏
  • 举报
回复
太谢谢了。
真的就通过了。为什么了?
masse 2004-12-15
  • 打赏
  • 举报
回复
换一下Expr类的位置
class Expr;



class Expr_node
{
friend ostream& operator << (ostream &, const Expr_node&);
friend class Expr;
int use;
//private:
protected:
Expr_node(): use( 1 ) {}
public:
virtual void print(ostream &) const = 0;
virtual ~Expr_node() {}

};

class Expr
{
public:
friend ostream &operator << ( ostream &, const Expr &);
Expr_node *p;
Expr( int );
Expr( const string &, Expr );
Expr( const string &, Expr, Expr );
Expr( const Expr &t)
{ p = t.p; ++p->use; }
Expr & operator = ( const Expr &t );
~Expr() { delete p; }
};

64,685

社区成员

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

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