前瞻宣言? [英] forward declaration?

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

问题描述

你好

,原始代码如下我得到错误:

"'struct CPlayer''"


类CPlayer; //这是做什么的?实例化CPlayer类?

//什么是前向声明?


class CEnemy:public CEntity

{

public:

CPlayer *播放器;

}


我试过这样就带来了错误:

"错误:'*''令牌之前的语法错误"


class CEnemy:public CEntity

{

public:

// CPlayer *播放器;

CPlayer *播放器=新CPlayer;

}


感谢您的帮助和问候。

Michael

解决方案

Michael Sgier写道:< blockquote class =post_quotes>你好
下面的原始代码我得到错误:
转发声明`struct CPlayer''"

类CPlayer; //这是做什么的?实例化CPlayer类?


否。它告诉编译器CPlayer是一个类。这是必要的,如果你有
有相互引用的类,或者你可以在一些

的情况下这样做,只是为了避免#including定义类的头,

可以加快编译速度。

//什么是前向声明?


上面的CPlayer声明是一个前向声明,即你已经将b $ b指定为完整类定义之前的类,这将是是

其他地方。

班级CEnemy:公共CEntity
{
公开:
CPlayer *播放器;
}


缺少分号。


必须有其他代码编译但未发布。 CEntity的

定义在哪里?另外,为了得到''结构CP层''的错误,建议说,CPlayer实际上是一个结构,而不是一个类。我猜你编译的来源

文件#include了CPlayer的完整定义,这是一个

结构,但是前面的声明说这是一个类,因此错误

消息。


如果你只引用指针或

引用CPlayer,你可以使用前向声明(这是这种情况),但在其他情况下你需要整个

定义的CPlayer,比如在CEnemy中定义一个成员对象

类型CPlayer(相反)到指针或引用),或访问

任何CPlayer成员等。

我试过这样就带来了错误:
错误: '*''标记'之前的语法错误


是的,因为编译器不知道CPlayer是什么。你需要CPlayer的前向

声明,CPlayer的完整定义,你需要

完整定义的CEntity。

class CEnemy:public CEntity
{
公开:
// CPlayer *播放器;
CPlayer *播放器=新CPlayer;
}




另一个缺少的分号。


DW


" David White" < no@email.provided>在消息中写道

news:vJ ****************** @ nasal.pacific.net.au ...

是的,因为编译器不知道CPlayer是什么。你需要一个
前向CPlayer声明,


更正:插入单词or或这里。

CPlayer的完整定义,你需要完整的CEntity定义。




DW


下面的文章演示了使用前向声明来简化头文件管理。

http://www.eventhelix.com/RealtimeMa...dePatterns.htm


-

EventStudio系统设计器2.5 - http://www.EventHelix.com/EventStudio

基于序列图的系统设计和建模工具


Hello
with the original code below I get the error:
"forward declaration of `struct CPlayer''"

class CPlayer; // what does this do? Instantiate the class CPlayer?
// what''s a forward declaration?

class CEnemy : public CEntity
{
public:
CPlayer *player;
}

I tried it like this which brought the error:
"error: syntax error before `*'' token"

class CEnemy : public CEntity
{
public:
// CPlayer *player;
CPlayer* player = new CPlayer;
}

THANKS for you help and regards.
Michael

解决方案

Michael Sgier wrote:

Hello
with the original code below I get the error:
"forward declaration of `struct CPlayer''"

class CPlayer; // what does this do? Instantiate the class CPlayer?
No. It tells the compiler that CPlayer is a class. This is necessary if you
have classes that refer to each other, or you can do it in some
circumstances simply to avoid #including the header that defines the class,
which can speed up compilation.
// what''s a forward declaration?
The declaration for CPlayer above is a forward declaration, i.e., you''ve
specified it as a class ahead of the full class definition, which will be
somewhere else.

class CEnemy : public CEntity
{
public:
CPlayer *player;
}
Missing semi-colon.

There must be other code that you compiled but did not post. Where is the
definition of CEntity? Also, to get an error for ''struct CPlayer'' suggests
that CPlayer is really a struct, not a class. I''m guessing that the source
file you compiled #includes the full definition of CPlayer, which is a
struct, but the forward declaration says it''s a class, hence the error
message.

You can use a forward declaration if you only refer to pointers or
references to CPlayer (which is the case here), but you need the entire
definition of CPlayer in other cases, such as defining a member object of
type CPlayer in CEnemy (as opposed to a pointer or reference), or accessing
any member of CPlayer, etc.

I tried it like this which brought the error:
"error: syntax error before `*'' token"
Yes, because the compiler does not know what CPlayer is. You need a forward
declaration of CPlayer, the full definition of CPlayer, and you need the
full definitionof CEntity.
class CEnemy : public CEntity
{
public:
// CPlayer *player;
CPlayer* player = new CPlayer;
}



Another missing semi-colon.

DW


"David White" <no@email.provided> wrote in message
news:vJ******************@nasal.pacific.net.au...

Yes, because the compiler does not know what CPlayer is. You need a forward declaration of CPlayer,
Correction: Insert the word "or" here.
the full definition of CPlayer, and you need the
full definitionof CEntity.



DW


The following article demonstrates the use of forward declarations to
simplify header file management.

http://www.eventhelix.com/RealtimeMa...dePatterns.htm

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
Sequence Diagram Based System Design and Modeling Tool


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

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