前向引用问题 [英] Forward Referencing Question

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

问题描述

大家好,


我有这个stange编译器错误。我有一个B类,其中包括指向A类的
指针,因此我做了前瞻性的参考:

A类;在头文件的开头。


好​​吧,这样可行,但如果我包括以下


B类{


A * p1;

A * p2;


B(A * a1,A * a2){p1 = a1; p2 = a2;}

bool operator ==(const C2Clique& p)const {return

(a1-> getId()== p。 a1-> getId;)


}


好​​的然后我收到一条错误消息,告诉我类型A未定义。我已经意识到这是因为我使用了A的> getId()方法在

中的==在B.


所以我的问题是,由于A依赖于B而反之亦然,我怎么能在这个问题上得到




Adam

解决方案

Adam Hartshorne写道:

大家好,

我有这个stange编译错误。我有一个B级,包括指向A级的指针,所以我做了A / A的前向引用;在头文件的开头。

好吧,这样可行,但如果我包括以下

B类{

A * p1 ;
A * p2;

B(A * a1,A * a2){p1 = a1; p2 = a2;}

bool operator ==(const C2Clique& p)const {return
(a1-> getId()== p.a1-> getId;)

}
好的,然后我收到一条错误消息,告诉我类型A未定义。我已经意识到这是因为我在B中的==使用了A的> getId()方法。

所以我的问题是A依赖在B上,反之亦然,我怎么能解决这个问题呢?




将operator ==的实现放入.cpp文件中。


Adam Hartshorne写道:


所以我的问题是A依赖B而反之亦然我怎样才能获得<围绕这个问题?


不要内联方法体。


例如。更改


A级{

....

void f(){...}

....

};

B级{...};


成:


A级{

....

void f();

.... < br $>
};

B级{...};


void A :: f(){...}


当然,你真正想做的事情(无论如何我会做)是定义

每个类的.h包含文件中的类(例如.Ah和Bh)和

为每个类创建一个实现(例如A.cpp和B.cpp)。然后在

你的Bh在课前包括Ah。

Adam



-

Peter MacMillan

电子邮件/ msn: pe***@writeopen.com

icq:1-874-927

GCS / IT / L d - ( - )> -pu s():( - )a- C +++(+++ +)与GT;


UL>

Hi All,

I have this stange compiler error. I have a class B that includes
pointers to class A, therefore I have done forward referencing by doing
class A; at the start of the header file.

Ok well that works ok, but if I include the following

class B {

A* p1 ;
A* p2 ;

B(A* a1, A* a2) {p1 = a1 ; p2 = a2 ;}

bool operator==(const C2Clique& p) const { return
(a1->getId()==p.a1->getId ;)

}

Ok then I get an error messaging saying me that type A is undefined. I
have realised this is because I am using the ->getId() method of A in
the == in B.

So my question is that as A relies on B and vice versa how can I get
around this problem?

Adam

解决方案

Adam Hartshorne wrote:

Hi All,

I have this stange compiler error. I have a class B that includes
pointers to class A, therefore I have done forward referencing by doing
class A; at the start of the header file.

Ok well that works ok, but if I include the following

class B {

A* p1 ;
A* p2 ;

B(A* a1, A* a2) {p1 = a1 ; p2 = a2 ;}

bool operator==(const C2Clique& p) const { return
(a1->getId()==p.a1->getId ;)

}

Ok then I get an error messaging saying me that type A is undefined. I
have realised this is because I am using the ->getId() method of A in
the == in B.

So my question is that as A relies on B and vice versa how can I get
around this problem?



Put the implementation of the operator== into the .cpp file.


Adam Hartshorne wrote:


So my question is that as A relies on B and vice versa how can I get
around this problem?

Don''t inline the method body.

eg. change

class A {
....
void f() { ... }
....
};
class B { ... };

into:

class A {
....
void f();
....
};
class B { ... };

void A::f() { ... }

Of course, what you really want to do (what I would do anyway) is define
the class in a .h include file for each class (eg. A.h and B.h) and
create an implementation for each class (eg. A.cpp and B.cpp). Then in
your B.h just include A.h before the class.
Adam


--
Peter MacMillan
e-mail/msn: pe***@writeopen.com
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>


UL>


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

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