繁体   English   中英

C ++转发声明(指针)-Access成员

[英]C++ Forward Declaration (Pointer) - Access member

我正在使用OpenGL和DirectX,并且已经开始开发面向对象游戏类的基础知识。 当前类的结构如下:

宾语
--- | ---演员
--------- | ---典当
--------- | ---控制器

所以我有继承ActorPawnController类, Actor继承了Object

问题是我需要在pawn类中有一个Controller的引用,在controller类中有一个Pawn的实例。
因此,我在Actor.hActor.h声明了PawnController

// Actor.h

// (...) Actor Declaration (...)

class Pawn;
class Controller;

然后在Pawn.h

// Pawn.h

class Pawn : public Actor
{
private:
    Controller* controller;
public:
    void DoSomethingWithController(void);
}

这一切都很好,没有错误,所有。 问题是当我想访问该类中的成员时:

void Pawn::DoSomethingWithController(void)
{
    // this->controller-> can't access members like this (members not found)
}

因此,我应该怎么做才能在Pawn中有一个Controller指针,在Controller中有一个Pawn指针,将它们保存在不同的文件(.h和.cpp)中,同时又可以访问其成员?

感谢您的时间。 :d

[如果需要更多信息,我会提供]

前向声明对编译器说,将进一步提供给定类型。 编译器不了解有关该类型的字段或成员的任何信息。 因此,您需要在.cpp文件(您访问“ controller”类的成员的位置)中包括相应的.h文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM