构造函数问题 [英] A constructor question

查看:61
本文介绍了构造函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,我不知道如何处理。我有两个

类,X和Y,每个我有一个构造函数,它将参考作为参数

作为参数,如下图所示。


class X {

private:

int a;

public:

X();

X(Y&);

};

等级Y {

私人:

int b;

public:

Y();

Y(X&);

};

当然,编译器抱怨因为Y在解析类X的

定义时未声明。


任何帮助表示赞赏。


-----

Jorn Attermann,

I have a simple problem which I don''t know how to handle. I have two
classes, X and Y, and in each I have a constructor which takes a reference
to the other as an argument as sketched below.

class X {
private:
int a;
public:
X();
X(Y&);
};
class Y {
private:
int b;
public:
Y();
Y(X&);
};
Naturally, the compiler complains because Y is undeclared when it parses the
definition of class X.

Any help is appreciated.

-----
Jorn Attermann,

推荐答案



" Jorn Attermann" <乔**** @ nospam.dk>在留言新闻中写道:3f *********************** @ dread11.news.tele.d k ...

"Jorn Attermann" <jo****@nospam.dk> wrote in message news:3f***********************@dread11.news.tele.d k...
I有一个简单的问题,我不知道如何处理。我有两个
类,X和Y,每个我都有一个构造函数,它引用另一个作为参数,如下图所示。
I have a simple problem which I don''t know how to handle. I have two
classes, X and Y, and in each I have a constructor which takes a reference
to the other as an argument as sketched below.



在定义X之前添加

类Y;

,反之亦然。

http://www.parashift.com/c++-faq-lit...html #faq-38.11


2003年10月14日星期二21:27:38 +0200,Jorn Attermann< jo **** @ nospam.dk> ;

写道:
On Tue, 14 Oct 2003 21:27:38 +0200, Jorn Attermann <jo****@nospam.dk>
wrote:
我有一个简单的问题,我不知道如何处理。我有两个类,X和Y,每一个我都有一个构造函数,它将
引用作为参数,如下图所示。

X级{
私人:
int a;
公开:
X();
X(Y&);
};

class Y {
私人:
int b;
公开:
Y();
Y(X&);
};

当然,编译器会抱怨,因为当它解析了X类的定义时,Y是未申报的。

任何帮助都表示赞赏。

-----
Jorn Attermann,
I have a simple problem which I don''t know how to handle. I have two
classes, X and Y, and in each I have a constructor which takes a
reference
to the other as an argument as sketched below.

class X {
private:
int a;
public:
X();
X(Y&);
};
class Y {
private:
int b;
public:
Y();
Y(X&);
};
Naturally, the compiler complains because Y is undeclared when it parses
the
definition of class X.

Any help is appreciated.

-----
Jorn Attermann,



需要转发声明。


X级;


级Y {声明/定义......};

级X {声明/定义......};


如果,让我们说,你想使用X作为Y的成员,那么X应首先定义为



如果反向然后反转。

在Y中使用X类成员并在X中使用Y类型成员是无意义的br />
相同类型。


-

grzegorz


Forward declaration is needed.

class X;

class Y { declarations/ definitions ... };
class X { declarations/defintiions ... };

If , let''s say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It''s nonsense to have type X member in Y and type Y member in X at the
same type.

--
grzegorz




< gr ********** @ pacbell.net>在消息中写道

news:op ************** @ news.sf.sbcglobal.net ...

<gr**********@pacbell.net> wrote in message
news:op**************@news.sf.sbcglobal.net...

如果,让我们说,你想使用X作为Y的成员,那么首先应该定义X.
如果反向然后反转。
这是无稽之谈在Y中使用X型成员,在相同类型的X中键入Y成员。

If , let''s say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It''s nonsense to have type X member in Y and type Y member in X at the
same type.




不一定。我可能会把一个盒子装进车里,或者我可能把一辆车放在一个盒子里。

我可能有一个有容器的记录,或者我可能有一个容器

有记录。



Not necessarily. I might put a box in car, or I might put a car in a box.
I might have a record that has a container, or I might have a container that
has a record.


这篇关于构造函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆